Reputation: 4774
JUnit4 has @FixMethodOrder
annotation which allows to use alphabetical order of test methods execution. Is there analogous JUnit5 mechanism?
Upvotes: 20
Views: 16851
Reputation: 24512
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
@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.
Upvotes: 0
Reputation: 1500
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.
Upvotes: 23
Reputation: 51040
No, 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.
Upvotes: 13