Junit4: Test method execution order

      In the previous version, Junit did not specify the execution order of test methods in design, and method calls were based on the return order of reflection api. Given that the Java platform does not stipulate a specific order, and even the order of its return has a certain randomness, it is very unwise to rely on the order of the JVM.

Of course, well-written test cases should not depend on the order in which test methods are executed. But what if this is the case? At this point, predictable test failures from "determined order" are always better than random test failures.

Starting with version 4.11, Junit will use a deterministic ordering by default, which is MethodSorters.DEFAULT. If you want to change the execution order of the test methods, you can add the FixMethodOrder annotation to the test case and specify the ordering type of its methods. The optional MethodSorters are:

  • DEFAULT, compare according to the hashCode of the method name;
  • NAME_ASCENDING, compare based on method name;
  • JVM, using the order returned by the JVM.

Example (official website, in fact no example):

 
 
  1. @FixMethodOrder(MethodSorters.NAME_ASCENDING)
  2. public class TestMethodOrder {   
  3.  
  4. @Test
  5. public void  testA () {  
  6. System.out.println("first");
  7. }
  8. @Test
  9. public void  testB () {  
  10. System.out.println("second");
  11. }
  12. @Test
  13. public void  testC () {  
  14. System.out.println("third");
  15. }
  16. }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324869504&siteId=291194637