JUnit 测试类顺序 [英] JUnit test class order
问题描述
我有一个带有 Maven 的 Java 应用程序.用于测试的 Junit,带有故障安全和万无一失的插件.我有 2000 多个集成测试.为了加速测试运行,我使用故障安全 jvmfork 来并行运行我的测试.我有一些繁重的测试类,它们通常在我的测试执行结束时运行,这会减慢我的 CI 验证过程.filesafe runorder:balanced 对我来说是一个不错的选择,但我不能使用它,因为 jvmfork.重命名测试类或移动到另一个包并运行它 alpahabetical 不是一个选项.有什么建议可以在验证过程开始时运行我的慢速测试类吗?
I have a java app with maven. Junit for tests, with failsafe and surefire plugins. I have more than 2000 integration tests. To speed up the test running, I use failsafe jvmfork to run my tests parallel. I have some heavy test class, and they typically running at end of my test execution and it is slows down my CI verify process. The filesafe runorder:balanced would be a good option for me, but i cant use it because the jvmfork. To rename the test classes or move to another package and run it alpahabetical is not an option. Any suggestion how can I run my slow test classes at the begining of the verify process?
推荐答案
在 JUnit 5 中(来自版本 5.8.0 以后)也可以订购测试类.
In JUnit 5 (from 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
其他 Junit 内置类排序器实现:
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
对于其他方式(除了 junit-platform.properties 文件)设置配置参数,请参见 JUnit 5 用户指南.
For other ways (beside junit-platform.properties file) to set configuration parameters see JUnit 5 user guide.
您也可以提供自己的订购者.它必须实现 ClassOrderer
接口:
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
测试类不能由 ClassOrderer
排序.
参考 JUnit 5 文档 和 ClassOrderer api 文档 了解更多相关信息.
Note that @Nested
test classes cannot be ordered by a ClassOrderer
.
Refer to JUnit 5 documentations and ClassOrderer api docs to learn more about this.
这篇关于JUnit 测试类顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!