1

I'm testing an app with espresso. The 1st testcase loads data with the 1st activity from the web into a local database. The 2nd test case / 2. Activity shows data from the database.

That's why Testcase 1 needs to run before Testcase 2. But that does not always happen, espresso occasionally changes the order. How can I solve the problem? Can I set the order of the TestCases (Testclasses)?

Jens Neitz
  • 117
  • 4

1 Answers1

5

You have the following annotation @FixMethodOrder.

You can use it with the following parameter : MethodSorters.NAME_ASCENDING.

The code (an example) :

@RunWith(AndroidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class YourTestClass {

   @Test
   public void A_Test() {
      System.out.println("1");
   }

   @Test
   public void B_Test() {
      System.out.println("2");
   }
}

You will find more detailed solutions here : [previous answers] (Test order with espresso)


You can do that using the @RunWith annotation. You can have a short look here Aggregating tests in suites. Basically what you have to do is the following :

Edit:

@RunWith(Suite.class)  
@Suite.SuiteClasses({  
    TestFeature1.class,  
    TestFeature2.class,  
    TestFeature3.class,  
    TestFeature4.class  
})  
public class FeatureTestSuite {  
// the class remains empty,  
// used only as a holder for the above annotations  
}
R13mus
  • 752
  • 11
  • 20
  • I mean, different classes: @RunWith(AndroidJUnit4.class) public class first_Test... @RunWith(AndroidJUnit4.class) public class second_Test... Is that even possible, or is my approach completely wrong? – Jens Neitz Sep 07 '18 at 10:10
  • You can do that using the @RunWith annotation. You can have a short look here [Aggregating tests in suites](https://github.com/junit-team/junit4/wiki/aggregating-tests-in-suites). Basically what you have to do is the following : `@RunWith(Suite.class) @Suite.SuiteClasses({ TestFeature1.class, TestFeature2.class, TestFeature3.class, TestFeature4.class }) public class FeatureTestSuite { // the class remains empty, // used only as a holder for the above annotations }` – R13mus Sep 11 '18 at 08:00
  • And how do you create a coverage report for this test suite? – Starwave Aug 08 '23 at 07:42