如何在JUnit5中更改测试执行顺序?

如何在JUnit5中更改测试执行顺序?

问题描述:

JUnit4具有@FixMethodOrder批注,该批注允许使用按字母顺序排列的测试方法执行顺序.有类似的JUnit5机制吗?

JUnit4 has @FixMethodOrder annotation which allows to use alphabetical order of test methods execution. Is there analogous JUnit5 mechanism?

JUnit 5.4现在正式发布,因此不再需要使用快照.

JUnit 5.4 is officially released now, so no need to use snapshots anymore.

现在可以通过JUnit 5.4实现.

This is now possible with JUnit 5.4.

https://junit.org /junit5/docs/current/user-guide/#writing-tests-test-execution-order

要控制执行测试方法的顺序,请注释您的 测试类或带有@TestMethodOrder的测试接口,并指定 所需的MethodOrderer实现.您可以实现自己的 自定义MethodOrderer或使用以下内置之一 MethodOrderer实现.

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:根据值对测试方法进行数字排序 通过@Order注释指定.

OrderAnnotation: sorts test methods numerically based on values specified via the @Order annotation.