How to run test methods in order with Junit

后端 未结 4 1793
一生所求
一生所求 2021-01-12 04:34

I am using JUnit and Selenium Webdriver. I want to run my test methods in order as how I write them in my code, as below:

@Test
public void registerUserTest(         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-12 05:03

    You can sort methods with @FixMethodOrder(MethodSorters.NAME_ASCENDING) annotation. Like,

    @FixMethodOrder(MethodSorters.DEFAULT)
    

    public class DefaultOrderOfExecutionTest { private static StringBuilder output = new StringBuilder("");

    @Test
    public void secondTest() {
        output.append("b");
    }
    
    @Test
    public void thirdTest() {
        output.append("c");
    }
    
    @Test
    public void firstTest() {
        output.append("a");
    }
    
    @AfterClass
    public static void assertOutput() {
        assertEquals(output.toString(), "cab");
    }
    

    }

    You can perform sorting in 3 ways:

    1. MethodSorters.DEFAULT- This default strategy compares test methods using their hashcodes. In case of a hash collision, the lexicographical order is used.
    2. MethodSorters.JVM- This strategy utilizes the natural JVM ordering – which can be different for each run.
    3. MethodSorters.NAME_ASCENDING- This strategy can be used for running test in their lexicographic order.

    For more details please refer:The Order of Tests in JUnit

提交回复
热议问题