Android UI testing - ActivityScenarioRule is significantly slower then ActivityScenario

737 Views Asked by At

I'm getting into Android UI testing, and I learnt two ways to start up activities: ActivityScenario in each test, and ActivityScenarioRule once, annotated with @Rule.

At first I used ActivityScenario, with success. Each test took around 2-4 second to complete:

@RunWith(AndroidJUnit4ClassRunner.class)
public class MyActivityTest {

    @Test
    public void test1() {
        ActivityScenario<MyActivity> scenario = ActivityScenario.launch(MyActivity.class);
        //do testing...
    }

    @Test
    public void test2() {
        ActivityScenario<MyActivity> scenario = ActivityScenario.launch(MyActivity.class);
        //do testing...
    }

    //more tests...
}

Then I decided to switch to an ActivityScenarioRule, since I have a decent amount of tests written, and with a rule, I don't need to create an ActivityScenario to each test:

@RunWith(AndroidJUnit4ClassRunner.class)
public class MyActivityTest {

   @Rule //launch this activity before each test
   public ActivityScenarioRule<MyActivity> rule = new ActivityScenarioRule<>(MyActivity.class);

    @Test
    public void test1() {
        //do testing...
    }

    @Test
    public void test2() {
        //do testing...
    }

    //more tests...
}

If I run the tests with this rule, each test takes around 40-50 seconds and most of it is spent on this screen:

enter image description here

Tests are run on an emulator. I don't know what this screen is and why it takes up so much time when using ActivityScenarioRule. When using ActivityScenario in each test, this screen does not show up and tests complete fast. Please help, what am I missing?

0

There are 0 best solutions below