有没有一种方法可以在安卓中设置测试运行顺序?
我使用Espresso框架,需要测试许多活动和它们之间的转换。我想为这些活动编写不同的测试,但是我需要运行这些测试的特定顺序。
有没有一种方法可以在安卓中设置测试运行顺序?
我使用Espresso框架,需要测试许多活动和它们之间的转换。我想为这些活动编写不同的测试,但是我需要运行这些测试的特定顺序。
浓缩咖啡设置测试的运行顺序。
Junit 4.11 引入了 @FixMethodOrder 注释。只需升级您的 junit 版本并使用 FixMethodOrder(MethodSorters.NAME_ASCENDING) 注释测试类即可,无需使用自定义解决方案。有关详细信息,请查看发行说明。
以下是示例:
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");
}
}
您可以像这样将注释添加为测试运行器装置:
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
班级名称上方
如@spinster所说,您应该以不考虑顺序的方式编写测试。
我相信Junit 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 {
}
方法三: Junit5的使用
使用@Order注解进行排序
@Test
@Order(2)
public void testFunction(){
}
您可以使用订单编号和测试名称来设置订单,以下是示例:
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());
}
}
我需要先测试登录活动,如果成功,它将登录用户。然后,我应该测试其他活动。注销活动测试应在最后运行。因此,需要按照活动测试的顺序进行。