如何使用 Espresso Kotlin 进行顺序测试
How to do Sequential Tests with Espresso Kotlin
所以我想让我的练习测试套件按顺序进行,也就是说,测试是相互叠加的。目前每次测试完成都会重新启动应用程序,但我希望应用程序保持打开状态。
我试过使用@BeforeAll 但它不起作用,它让我将 JUnit5.4 添加到 class 路径,甚至在我这样做之后它仍然是红色的,这意味着它对某些人来说不喜欢它原因。
无论如何,我认为这是规则,我认为 activity 让我的测试在每次完成时都重新启动,我希望它不这样做,或者如果有不同的测试规则我可以使用那不这样做那就太好了。
class Practice {
@get:Rule
val activityRule = ActivityScenarioRule(MainActivity::class.java);
@Before
fun setUp() {
onView(withText("Log In With Mobile Code")).perform(click());
onView(withResourceName("txtMobileAccessCode")).check(matches(withText("Company Code
or\nMobile Access Code")));
}
@Test
fun clickOnEnterAccessCode() {
onView(withResourceName("txtCodeEntry")).perform(typeText("CodeGoesHere"));
}
@Test
fun enterCode() {
onView(withResourceName("btnCodeSubmit")).perform(click());
}
}
问题在于使用 ActivityScenarioRule
来驱动;默认情况下,这会在每次测试结束时调用 ActivityScenario.close()
,这将重置您的应用。
相反,通过直接处理 ActivityScenario
来查看自己控制 activity 的生命周期。会有更多的开销,但你将有更多的控制权并且能够不调用close()
直到你想要。
从 Android Developer Docs 中,您要查找的语法如下:
val scenario = launchActivity<MainActivity>()
谢谢 Mike Collins,没有你我做不到。
这是最终产品,这样可以使我的整个套件按顺序排列我只需要在第一个测试中声明场景,其余的继续使用 activity.
@Before
fun setUp() {
val scenario = launchActivity<MainActivity>()
Espresso.onView(ViewMatchers.withText("Log In With Mobile
Code")).perform(ViewActions.scrollTo())
Espresso.onView(ViewMatchers.withText("Log In With Mobile
Code")).perform(ViewActions.click());
Espresso.onView(ViewMatchers.withResourceName("txtMobileAccessCode"))
.check(ViewAssertions.matches(ViewMatchers.withText("Company Code or\nMobile
Access Code")));
}
有了这个我什至不必使用
@get:Rule
val activityRule = ActivityScenarioRulePractice(MainActivity::class.java);
我可以把它统统省掉
class Test1 {
@Before
fun setUp() {
val scenario = launchActivity<MainActivity>()
Espresso.onView(ViewMatchers.withText("Log In With Mobile Code")).perform(ViewActions.scrollTo())
Espresso.onView(ViewMatchers.withText("Log In With Mobile Code")).perform(ViewActions.click());
Espresso.onView(ViewMatchers.withResourceName("txtMobileAccessCode")).check(ViewAssertions.matches(ViewMatchers.withText("Company Code or\nMobile Access Code")));
}
@Test
fun clickOnEnterAccessCode() {
Espresso.onView(ViewMatchers.withResourceName("txtCodeEntry")).perform(ViewActions.typeText("Bluebook"));
Espresso.onView(ViewMatchers.withResourceName("btnCodeSubmit")).perform(ViewActions.click());
Espresso.onView(ViewMatchers.withText("<b>Network connection unavailable</b><br/>\n" +
"Please check your network settings and try again.")).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
}
@After
fun cleanUp() {
reportHelper.label("Stopping App");
}
}
我可能必须在@After 上添加一些内容,但到目前为止它会自动关闭所有内容。
所以我想让我的练习测试套件按顺序进行,也就是说,测试是相互叠加的。目前每次测试完成都会重新启动应用程序,但我希望应用程序保持打开状态。
我试过使用@BeforeAll 但它不起作用,它让我将 JUnit5.4 添加到 class 路径,甚至在我这样做之后它仍然是红色的,这意味着它对某些人来说不喜欢它原因。
无论如何,我认为这是规则,我认为 activity 让我的测试在每次完成时都重新启动,我希望它不这样做,或者如果有不同的测试规则我可以使用那不这样做那就太好了。
class Practice {
@get:Rule
val activityRule = ActivityScenarioRule(MainActivity::class.java);
@Before
fun setUp() {
onView(withText("Log In With Mobile Code")).perform(click());
onView(withResourceName("txtMobileAccessCode")).check(matches(withText("Company Code
or\nMobile Access Code")));
}
@Test
fun clickOnEnterAccessCode() {
onView(withResourceName("txtCodeEntry")).perform(typeText("CodeGoesHere"));
}
@Test
fun enterCode() {
onView(withResourceName("btnCodeSubmit")).perform(click());
}
}
问题在于使用 ActivityScenarioRule
来驱动;默认情况下,这会在每次测试结束时调用 ActivityScenario.close()
,这将重置您的应用。
相反,通过直接处理 ActivityScenario
来查看自己控制 activity 的生命周期。会有更多的开销,但你将有更多的控制权并且能够不调用close()
直到你想要。
从 Android Developer Docs 中,您要查找的语法如下:
val scenario = launchActivity<MainActivity>()
谢谢 Mike Collins,没有你我做不到。 这是最终产品,这样可以使我的整个套件按顺序排列我只需要在第一个测试中声明场景,其余的继续使用 activity.
@Before
fun setUp() {
val scenario = launchActivity<MainActivity>()
Espresso.onView(ViewMatchers.withText("Log In With Mobile
Code")).perform(ViewActions.scrollTo())
Espresso.onView(ViewMatchers.withText("Log In With Mobile
Code")).perform(ViewActions.click());
Espresso.onView(ViewMatchers.withResourceName("txtMobileAccessCode"))
.check(ViewAssertions.matches(ViewMatchers.withText("Company Code or\nMobile
Access Code")));
}
有了这个我什至不必使用
@get:Rule
val activityRule = ActivityScenarioRulePractice(MainActivity::class.java);
我可以把它统统省掉
class Test1 {
@Before
fun setUp() {
val scenario = launchActivity<MainActivity>()
Espresso.onView(ViewMatchers.withText("Log In With Mobile Code")).perform(ViewActions.scrollTo())
Espresso.onView(ViewMatchers.withText("Log In With Mobile Code")).perform(ViewActions.click());
Espresso.onView(ViewMatchers.withResourceName("txtMobileAccessCode")).check(ViewAssertions.matches(ViewMatchers.withText("Company Code or\nMobile Access Code")));
}
@Test
fun clickOnEnterAccessCode() {
Espresso.onView(ViewMatchers.withResourceName("txtCodeEntry")).perform(ViewActions.typeText("Bluebook"));
Espresso.onView(ViewMatchers.withResourceName("btnCodeSubmit")).perform(ViewActions.click());
Espresso.onView(ViewMatchers.withText("<b>Network connection unavailable</b><br/>\n" +
"Please check your network settings and try again.")).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
}
@After
fun cleanUp() {
reportHelper.label("Stopping App");
}
}
我可能必须在@After 上添加一些内容,但到目前为止它会自动关闭所有内容。