Search code examples
androidunit-testingjunitandroid-testingandroid-instrumentation

Android instrumented test infinitely running


I've written up some instrumented tests, and the assertions are passing - however regardless of what configuration they are run in, the run never terminates/ends - I have to manually terminate the run - which is not optimal for a CI setup.

I have tried running the below code in the following run configurations:

  • Run all tests in the test class - in this instance the run does not progress onto the second test after running the first
  • Run all tests in test package - in this instance the run does not progress onto the second test after running the first, but all other instrumented tests in other classes succeed.
  • Run both tests in the test class separately

Even if I take out my test code and put in incredibly simple tests (as I have done below) the code execution does not complete.

public class TestClass {

    private MainActivity mainActivity;
    private MyScreen myScreenInstance;

    @Rule
    public ActivityTestRule mActivityRule = new ActivityTestRule<>(MainActivity.class);

    @Before
    public void setUp() throws FileNotFoundException {
        mainActivity = (MainActivity) mActivityRule.getActivity();
    }

    @Test
    public void test_1() throws FileNotFoundException {
        assertEquals(1,2);
    }

    @Test
    public void test_2() throws FileNotFoundException {
        assertEquals(2,2);
    }


}

For context I'm trying to test that a touch behaviour in a class I have written on top of a simple game engine written by a lecturer, the test code and assertions I have written all work - but the problem is that the run does not complete.

Do I have to have some sort of 'teardown' method to destroy the ActivityTestRule? What can I do to ensure this run completes correctly?

Interestingly these very simple assertions successfully run on API 25 on a Nexus 7 emulator, however when run in a Nexus 5 emulator running API 23 the tests do not complete as described above.

Further information:

  • running the tests slowly through with the debugger, stepping through steps - they all pass as they should
  • adding a Thread.sleep(1000) to the @After (tearDown) method, the described issue stops occurring. However this does not resolve this issue as it our run still times out in firebase test lab (CI)

Below is a picture of a run I started, and then 5 minutes later it's hung. It should have completed 2 tests in < 2 seconds.

enter image description here


Solution

  • I have now resolved this issue, ordinarily a developer would be adding a onView call, however due to the nature of the framework i am working in I was not doing so. I have added

    onView(withId(R.id.activity_fragment_id)).perform(click());
    

    to the setup, to just click the main area that the game runs within and now tests pass.