Search code examples
androidandroid-espressoinstrumented-test

How to set an order in instrumented test over the Test Class?


I'd like to set an order over the Test Class.

@RunWith(AndroidJUnit4::class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class ATest {
  @Test
  fun test0000()

  @Test
  fun test0001()
}
@RunWith(AndroidJUnit4::class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class BTest {
  @Test
  fun test0002()

  @Test
  fun test0003()
}

I'd like to test ATest.test0000 -> ATest.test0001 -> BTest.test0002 -> BTest.test0003

Because ATest class must be tested before B Test. How can I do that? Is it possible?


Solution

  • Firstly, I would recommend you to not have any dependencies in Tests.
    i.e. Test A class and Test B class should run independently from each other.
    This really helps when your application grows.

    There should not be a condition that one test should run before another.
    Only in a rare / genuine scenario we should have such dependency on sequence.

    Because if you design your test with sequence related dependency then it will be difficult for you to maintain your test cases and it will get difficult when you follow Test Driven Development(TDD).

    For above case, please try using SuiteClasses.
    The SuiteClasses annotation specifies the Suite runner which test classes to include in this suite and in which order.

    Please refer to the sample provided by Junit Team HERE