Android Espresso - how to run setup just once for all tests

I am using Espresso / Kotlin to run tests for our android app and I want to run the install once for all tests in a given test class.

I created a companion object to run the application once (which it does), however it then closes and does not stay open while each test is running.

enter image description here

How can I start the application, run all tests in the test class, and then close the application?

I've also tried the following, but it still starts once, then closes, then tries to run the tests: enter image description here

+3


source to share


1 answer


This is by design.

This rule provides functional testing of a single activity. Activity Testing will run before every test annotated with @Test and before any method annotated with @Before. It will complete after the test completes and all methods annotated with @After have finished. During a test, you can access the activity under test by calling ActivityTestRule.getActivity ().

Source: JUnit4 Rules



You may be able to work around this by creating your own rule. Otherwise, you can create one @Test

and put each of your statements in it. To keep the general format, you can put your statements in separate private functions.

For example:

@Test
fun testLoginPage() {
    testLoginButtonIsDisplayed()
    // call other private functions
}

private fun testLoginButtonIsDisplayed() {
    loginPage.loginButton.check(matches(isDisplayed()))
}

//  add other private functions

      

+1


source







All Articles