How to run test methods in specific order in JUnit4?

后端 未结 18 1843
伪装坚强ぢ
伪装坚强ぢ 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 04:56

    JUnit since 5.5 allows @TestMethodOrder(OrderAnnotation.class) on class and @Order(1) on test-methods.

    JUnit old versions allow test methods run ordering using class annotations:

    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    @FixMethodOrder(MethodSorters.JVM)
    @FixMethodOrder(MethodSorters.DEFAULT)
    

    By default test methods are run in alphabetical order. So, to set specific methods order you can name them like:

    a_TestWorkUnit_WithCertainState_ShouldDoSomething b_TestWorkUnit_WithCertainState_ShouldDoSomething c_TestWorkUnit_WithCertainState_ShouldDoSomething

    Or

    _1_TestWorkUnit_WithCertainState_ShouldDoSomething _2_TestWorkUnit_WithCertainState_ShouldDoSomething _3_TestWorkUnit_WithCertainState_ShouldDoSomething

    You can find examples here.

提交回复
热议问题