How to run test methods in specific order in JUnit4?

后端 未结 18 1836
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 04:35

I want to execute test methods which are annotated by @Test in specific order.

For example:

public class MyTest {
    @Test public void          


        
相关标签:
18条回答
  • 2020-11-22 05:10

    If the order is important, you should make the order yourself.

    @Test public void test1() { ... }
    @Test public void test2() { test1(); ... }
    

    In particular, you should list some or all possible order permutations to test, if necessary.

    For example,

    void test1(); 
    void test2(); 
    void test3(); 
    
    
    @Test
    public void testOrder1() { test1(); test3(); }
    
    @Test(expected = Exception.class)
    public void testOrder2() { test2(); test3(); test1(); }
    
    @Test(expected = NullPointerException.class)
    public void testOrder3() { test3(); test1(); test2(); }
    

    Or, a full test of all permutations:

    @Test
    public void testAllOrders() {
        for (Object[] sample: permute(1, 2, 3)) {
            for (Object index: sample) {
                switch (((Integer) index).intValue()) {
                    case 1: test1(); break; 
                    case 2: test2(); break; 
                    case 3: test3(); break; 
                }
            }
        }
    }
    

    Here, permute() is a simple function which iterates all possible permuations into a Collection of array.

    0 讨论(0)
  • 2020-11-22 05:10

    Please check out this one: https://github.com/TransparentMarket/junit. It runs the test in the order they are specified (defined within the compiled class file). Also it features a AllTests suite to run tests defined by sub package first. Using the AllTests implementation one can extend the solution in also filtering for properties (we used to use @Fast annotations but those were not published yet).

    0 讨论(0)
  • 2020-11-22 05:11

    What you want is perfectly reasonable when test cases are being run as a suite.

    Unfortunately no time to give a complete solution right now, but have a look at class:

    org.junit.runners.Suite
    

    Which allows you to call test cases (from any test class) in a specific order.

    These might be used to create functional, integration or system tests.

    This leaves your unit tests as they are without specific order (as recommended), whether you run them like that or not, and then re-use the tests as part of a bigger picture.

    We re-use/inherit the same code for unit, integration and system tests, sometimes data driven, sometimes commit driven, and sometimes run as a suite.

    0 讨论(0)
  • 2020-11-22 05:12

    Migration to TestNG seems the best way, but I see no clear solution here for jUnit. Here is most readable solution / formatting I found for jUnit:

    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    public class SampleTest {
        @Test
        void stage1_prepareAndTest(){};
    
        @Test
        void stage2_checkSomething(){};
    
        @Test
        void stage2_checkSomethingElse(){};
    
        @Test
        void stage3_thisDependsOnStage2(){};
    
        @Test
        void callTimeDoesntMatter(){}
    }
    

    This ensures stage2 methods are called after stage1 ones and before stage3 ones.

    0 讨论(0)
  • 2020-11-22 05:13

    I've read a few answers and agree its not best practice, but the easiest way to order your tests - and the way that JUnit runs tests by default is by alphabetic name ascending.

    So just name your tests in the alphabetic order that you want. Also note the test name must begin with the word test. Just watch out for numbers

    test12 will run before test2

    so:

    testA_MyFirstTest testC_ThirdTest testB_ATestThatRunsSecond

    0 讨论(0)
  • 2020-11-22 05:15

    With JUnit 5.4, you can specify the order :

    @Test
    @Order(2)
    public void sendEmailTestGmail() throws MessagingException {
    

    you just need to annotate your class

    @TestMethodOrder(OrderAnnotation.class)
    

    https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-execution-order

    i'm using this in my project and it works very well !

    0 讨论(0)
提交回复
热议问题