Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using Espresso/Kotlin to run tests for our Android Application and I want to run the setup once for all tests in the given test class.

I created a companion object to launch the application once (which it does), however it then closes and doesn't stay open while each test runs.

enter image description here

How can I have it launch the application, run all the tests in the test class, then close the application?

I also tried the following, but it still launches once then closes, then tries running the tests: enter image description here

like image 581
reutsey Avatar asked Oct 30 '22 04:10

reutsey


1 Answers

This is by design.

This rule provides functional testing of a single activity. The activity under test will be launched before each test annotated with @Test and before any method annotated with @Before. It will be terminated after the test is completed and all methods annotated with @After are finished. The activity under test can be accessed during your test by calling ActivityTestRule.getActivity().

Source: JUnit4 Rules

You might be able to get around it by making a custom rule. Otherwise, you could create a single @Test and put each of your assertions inside it. To keep your general format, you could put your assertions 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
like image 173
Derek Fredrickson Avatar answered Nov 07 '22 20:11

Derek Fredrickson