Execute order for test suite in junit

2019-06-20 05:11发布

问题:

I am having a test suite which is having the following structure

TestClass1
      - testmethod1()
      - testmethod2()
      - testmethod3()
      - testmethod4() 
TestClass2
          - testmethod11()
          - testmethod22()
          - testmethod33()
          - testmethod44()

In the above structure i want to execute the testmethod4() as the final one. ie) executed at last. There is a annotation @FixMethodOrder which executes a method in order not the testclass. Is there any mechanism to maintain order in test class and testmethod together. With the @FixMethodOrder i can execute the method by renaming the name of the test method but i can't instruct junit to execute the test class as the final one(last one).

回答1:

Though quoting @Andy again -

You shouldn't care about test ordering. If it's important, you've got interdependencies between tests, so you're testing behaviour + interdependencies, not simply behaviour. Your tests should work identically when executed in any order.

But if the need be to do so, you can try out Suite

@RunWith(Suite.class)

@Suite.SuiteClasses({
        TestClass2.class,
        TestClass1.class
})
public class JunitSuiteTest {
}

where you can either specify

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestClass1 {

    @AfterClass
    public void testMethod4() {

and then take care to name your method testMethod4 as such to be executed at the end OR you can also use @AfterClass which could soon be replaced by @AfterAll in Junit5.

Do take a look at Controlling the Order of the JUnit test by Alan Harder



回答2:

@shiriam as @Andy Turner already pointed out, the order of your tests shouldn't come in question when running the tests.

If you have a routine that you want executed before doing any tests, you could use a static block of code in one of the classes.

Think of something like this:

class TestBootstrap {
  // singleton instance
  private static final instance;
  private boolean initialized;

  private TestBootstrap(){
      this.initialized = false;    
  }

  public static TestBootstrap getInstance(){
       if (instance == null){
           instance = new TestBootstrap()
       }

  }
  public void init(){
      // make the method idempotent
      if (!initialzed){
         // do init stuff
         initialized = true;
      }
  }

  public boolean isInitialized(){
     return initialized;
  }

}

Then in your tests use something like this:

class TestClass1{
    @BeforeClass
    public void setup(){
         TestBootstrap.getInstance().init();
    }


    @Test
    public void testmethod1(){
        // assertions
    }

    // ....

}

class TestClass2{
    @BeforeClass
    public void setup(){
         TestBootstrap.getInstance().init();
    }

    @Test
    public void testmethod11(){
        // assertions
    }

    // ...
}

By using the singleton instance for doing the setup for the tests you ensure that you perform the initialization of your test environment only once, independently of the order in which the test classes are executed.



标签: java junit