How to run test methods in specific order in JUnit4?

后端 未结 18 1861
伪装坚强ぢ
伪装坚强ぢ 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:50

    The (as yet unreleased) change https://github.com/junit-team/junit/pull/386 introduces a @SortMethodsWith. https://github.com/junit-team/junit/pull/293 at least made the order predictable without that (in Java 7 it can be quite random).

    0 讨论(0)
  • 2020-11-22 04:53

    See my solution here: "Junit and java 7."

    In this article I describe how to run junit tests in order - "just as in your source code". Tests will be run, in order as your test methods appears in class file.

    http://intellijava.blogspot.com/2012/05/junit-and-java-7.html

    But as Pascal Thivent said, this is not a good practise.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 04:56

    Not sure I agree, If I want to test 'File Upload' and then test 'Data Inserted by File Upload' why would I not want these to be independent from each other? Perfectly reasonable I think to be able to run them separately rather than having both in a Goliath test case.

    0 讨论(0)
  • 2020-11-22 04:58

    Its one of the main issue which I faced when I worked on Junit and I came up with following solution which works fine for me:

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    import org.junit.runners.BlockJUnit4ClassRunner;
    import org.junit.runners.model.FrameworkMethod;
    import org.junit.runners.model.InitializationError;
    
    public class OrderedRunner extends BlockJUnit4ClassRunner {
    
        public OrderedRunner(Class<?> clazz) throws InitializationError {
            super(clazz);
        }
    
        @Override
        protected List<FrameworkMethod> computeTestMethods() {
            List<FrameworkMethod> list = super.computeTestMethods();
            List<FrameworkMethod> copy = new ArrayList<FrameworkMethod>(list);
            Collections.sort(copy, new Comparator<FrameworkMethod>() {
    
                @Override
                public int compare(FrameworkMethod f1, FrameworkMethod f2) {
                    Order o1 = f1.getAnnotation(Order.class);
                    Order o2 = f2.getAnnotation(Order.class);
    
                    if (o1 == null || o2 == null) {
                        return -1;
                    }
    
                    return o1.order() - o2.order();
                }
            });
            return copy;
        }
    }
    

    also create a interface like below:

     @Retention(RetentionPolicy.RUNTIME)
    
    
    @Target({ ElementType.METHOD})
    
    public @interface Order {
    public int order();
    }
    

    Now suppose you have class A where you have written several test cases like below:

    (@runWith=OrderRunner.class)
    Class A{
    @Test
    @Order(order = 1)
    
    void method(){
    
    //do something
    
    }
    
    }
    

    So execution will start from method named "method()". Thanks!

    0 讨论(0)
  • 2020-11-22 04:59

    If you get rid of your existing instance of Junit, and download JUnit 4.11 or greater in the build path, the following code will execute the test methods in the order of their names, sorted in ascending order:

    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    public class SampleTest {
    
        @Test
        public void testAcreate() {
            System.out.println("first");
        }
        @Test
        public void testBupdate() {
            System.out.println("second");
        }
        @Test
        public void testCdelete() {
            System.out.println("third");
        }
    }
    
    0 讨论(0)
提交回复
热议问题