JUnit4 has @FixMethodOrder annotation which allows to use alphabetical order of test methods execution. Is there analogous JUnit5 mechanism?
-
look at this answer: stackoverflow.com/a/54393573/7705712Edward D. Wilson– Edward D. Wilson2019-04-22 09:39:45 +00:00Commented Apr 22, 2019 at 9:39
4 Answers
Edit: JUnit 5.4 is officially released now, so no need to use snapshots anymore.
This is now possible with JUnit 5.4.
https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-execution-order
To control the order in which test methods are executed, annotate your test class or test interface with @TestMethodOrder and specify the desired MethodOrderer implementation. You can implement your own custom MethodOrderer or use one of the following built-in MethodOrderer implementations.
Alphanumeric: sorts test methods alphanumerically based on their names and formal parameter lists.
OrderAnnotation: sorts test methods numerically based on values specified via the @Order annotation.
4 Comments
dependencies { testImplementation "org.junit.jupiter:junit-jupiter-api:5.4.0-SNAPSHOT" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.4.0-SNAPSHOT" } is enoughNo, not yet. For unit tests, execution order should be irrelevant. For more complex tests, JUnit is aiming to provide explicit support - test ordering would be part of that.
7 Comments
With version 5.8.0 onwards, test classes can be ordered too.
src/test/resources/junit-platform.properties:
# ClassOrderer$OrderAnnotation sorts classes based on their @Order annotation
junit.jupiter.testclass.order.default=org.junit.jupiter.api.ClassOrderer$OrderAnnotation
Other Junit built-in class orderer implementations:
org.junit.jupiter.api.ClassOrderer$ClassName
org.junit.jupiter.api.ClassOrderer$DisplayName
org.junit.jupiter.api.ClassOrderer$Random
For other ways (beside junit-platform.properties file) to set configuration parameters refer here.
You can also provide your own orderer. It must implement ClassOrderer interface:
package foo;
public class MyOrderer implements ClassOrderer {
@Override
public void orderClasses(ClassOrdererContext context) {
Collections.shuffle(context.getClassDescriptors());
}
}
junit.jupiter.testclass.order.default=foo.MyOrderer
Note that @Nested test classes cannot be ordered by a ClassOrderer.
Refer to JUnit 5 documentations and ClassOrderer api docs to learn more about ordering test classes.
Comments
The answer to this question is sub.version dependent. In my environment, junit-jupiter-5.5.2, the implementation of ASCII test ordering that works for me is:
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.MethodOrderer.Alphanumeric;
@TestMethodOrder(Alphanumeric.class)
Related orderings (e.g. explicit numbering) are documented in the JUnit 5.5.2 API.
As of JUnit 5.7, I believe the amended declarations are:
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.MethodOrderer.MethodName;
@TestMethodOrder(MethodName.class)
If somebody with Junit 5.7 or later wants to test this, confirmation or correction would be welcome.