Original article: test execution order
Original Article Date: January 1, December 06, 2012
Translated on: February 1, October 16, 2014
Interpreter: bailima
According to the design, JUnit does not specify the execution sequence of the test method.
So far, these test methods are simply executed based on the order returned by the reflection API.
However, since the Java platform does not specify a clear sequence, it is unwise to use JVM to determine the sequence of the test method.
In fact, JDK 7 returns a random order.
Of course, writing a complete test code does not need to assume any execution order, but it is also necessary, and it is better to have a predictable error on some specific platforms than a random error.
From version 4.11, JUnit uses a deterministic but unpredictable sequence (methodsorters. Default) by default ).
If you want to change the execution sequence of test, you can simply add an annotation @ fixmethodorder on the test class and specify a suitable methodsorters.
@ Fixmethodorder (methodsorters. JVM): The execution sequence of the test method is determined based on the sequence returned by the JVM. The order of each test may be different.
@ Fixmethodorder (methodsorters. name_ascending): sort the method names alphabetically in ascending order.
Note: 1. junit4.11 and later versions are supported. 2. Using JUnit requires a hamcrest package.
Execution sequence of @ test in JUnit