有没有办法在android中设置测试运行顺序?
我使用Espresso框架,需要测试很多活动并在它们之间进行转换。我想为这些活动编写不同的测试,但我需要一个特定的命令来运行这些测试。
答案 0 :(得分:31)
espresso set运行测试顺序
来自Junit 4.11附带@FixMethodOrder注释。不使用自定义解决方案,只需使用FixMethodOrder(MethodSorters.NAME_ASCENDING)升级junit版本并注释测试类。查看发行说明以获取详细信息。
以下是一个示例:
import org.junit.runners.MethodSorters;
import org.junit.FixMethodOrder;
import org.junit.Test;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SampleTest {
@Test
public void A_firstTest() {
System.out.println("first");
}
@Test
public void B_secondTest() {
System.out.println("second");
}
}
答案 1 :(得分:9)
正如@spinster上面所说,你应该以顺序无关紧要的方式编写测试。
我相信Junit 3将按照完全限定类名的字母顺序运行测试,所以理论上你可以按照你希望它们执行的顺序按字母顺序命名它们(包名,类名,方法名)来控制顺序,但我不建议这样做。
请参阅: How to run test methods in specific order in JUnit4? How to pre-define the running sequences of junit test cases?
答案 2 :(得分:9)
您可以添加注释作为测试运行器夹具,如下所示:
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
就在班级名称
之上答案 3 :(得分:4)
是您可以使用test_name的订单号设置订单,请参阅以下示例 -
public class MyEspressoTest
extends ActivityInstrumentationTestCase2<UserLoginActivity> {
private UserLoginActivity mActivity;
public MyEspressoTest() {
super(UserLoginActivity.class);
}
@Before
public void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
mActivity = getActivity();
}
public void test1InvalidPropigerLogin() {
// Type text and then press the button.
//setContentView function to see the layout
onView(withId(R.id.username))
.perform(typeText("hill.hacker@gmail.com"), closeSoftKeyboard());
onView(withId(R.id.password))
.perform(typeText("hhhhh"), closeSoftKeyboard());
onView(withId(R.id.user_login_button)).perform(click());
// Check that the text was changed.
onView(withId(R.id.login_status))
.check(matches(withText("Invalid username or password")));
//System.out.println("Test pass with invalid user and password");
}
public void test2ValidPropigerLogin() {
// Type text and then press the button.
onView(withId(R.id.username))
.perform(typeText("hill.hacker@like.com"), closeSoftKeyboard());
onView(withId(R.id.password))
.perform(typeText("gggggg"), closeSoftKeyboard());
onView(withId(R.id.user_login_button)).perform(click());
//System.out.println("Test pass with valid user and password");
}
public void test3ForgetPasswordButton() {
onView(withId(R.id.forgot_pwd_button)).perform(click());
//onView(isRoot()).perform(ViewActions.pressBack());
onView(withId(R.id.email_edittext))
.perform(typeText("hill.hacker@propiger.in"), closeSoftKeyboard());
onView(withId(R.id.reset_password_button)).perform(click());
// Check that the text was changed.
onView(withId(R.id.reset_result))
.check(matches(withText("Email not registered with propiger")));
}
public void test4ForgetPasswordButton2() {
onView(withId(R.id.forgot_pwd_button)).perform(click());
onView(withId(R.id.email_edittext))
.perform(typeText("Hill.Hacker@like.com"), closeSoftKeyboard());
onView(withId(R.id.reset_password_button)).perform(click());
// Check that the text was changed.
onView(withId(R.id.reset_result))
.check(matches(withText("Reset password link sent successfully")));
}
public void test5RegisterButton() {
onView(withId(R.id.register_button)).perform(click());
//onView(isRoot()).perform(ViewActions.pressBack());
onView(withId(R.id.register_name_edittext))
.perform(typeText("Hill Hacker"), closeSoftKeyboard());
onView(withId(R.id.register_email_edittext))
.perform(typeText("Hill.Hacker+888@gmail.com"), closeSoftKeyboard());
onView(withId(R.id.register_mobileno_edittext))
.perform(typeText("9090909090"), closeSoftKeyboard());
onView(withId(R.id.register_password_edittext))
.perform(typeText("password111"), closeSoftKeyboard());
onView(withId(R.id.register_confirm_password_edittext))
.perform(typeText("password111"), closeSoftKeyboard());
//onView(withId(R.id.register_country_spinner)).perform(click());
//onView(isRoot()).perform(withId(R.id.register_country_spinner, Sampling.SECONDS_15));
onData(allOf(is(instanceOf(String.class)), is("India")))
.perform(click());
onView(withId(R.id.register_country_spinner)).check(matches(withText(containsString("India"))));
onView(withId(R.id.register_button)).perform(click());
}
}
答案 4 :(得分:2)
我需要首先测试loginActivity测试,如果成功,它将登录用户。然后我应该测试其他活动。 LogoutActivity测试应该在最后运行。因此,需要进行活动测试。
答案 5 :(得分:1)
在类名顶部添加注释 @FixMethodOrder(MethodSorters.NAME_ASCENDING),并按升序命名方法。
请参阅以下链接。答案是满足您的需求。
答案 6 :(得分:1)
您有3种方法:
方法1 :JUnit 4和5可以正常工作
@Test
public void testFunctionMain() {
test1();
test2()
test3();
}
方法2: JUnit 4和5可以正常工作
使用@FixMethodOrder
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@RunWith(AndroidJUnit4::class)
class LoginActivityTest {
}
方法3 :Junit5工作
使用@Order
@Test
@Order(2)
public void testFunction(){
}