我的代码中有两种类型的测试,以 UnitTest 和 IntegrationTest 结尾。当然,有一些遗留的 JUnit 4 测试和一些应该用 JUnit 5 编写的新测试。
我想要的:
可以从 IDE (IntelliJ IDEA) 运行的 UnitTestSuite 和 IntegrationTestSuit 类,它们每个都有按测试结束的类名筛选。此外,我希望两个不同的 gradle 任务各自运行自己的一组测试(理想情况下基于套装,或者至少基于类名)。
我尝试过的:
这个测试套件在 IDE 中运行良好,据我所知,它应该同时运行 JUnit 4 和 JUnit 5 测试。但是,这种方法似乎更像是一种解决方法,而不是实际的套件支持。
@RunWith(JUnitPlatform.class)
@IncludeClassNamePatterns({ "^.*UnitTest$" })
public class UnitTestSuite {
}
我还创建了这个 Gradle 任务,但它没有运行任何测试,对我说:
警告:忽略使用 JUnitPlatform runner 的测试类
test { Test t ->
useJUnitPlatform()
include "UnitTestSuite.class"
}
那么是否有同时运行 JUnit 4 和 JUnit 5 测试的解决方案,从 IDE 和 Gradle 任务中按名称过滤(收集到套装中)?
最佳答案
在 Gradle 中,您可以配置多个 test
任务,一个用于 JUnit 4,一个用于 JUnit 5。
我在 Spring Framework 构建中正是这样做的。请参阅 spring-test.gradle 中的 testJUnitJupiter
和 test
任务.
task testJUnitJupiter(type: Test) {
description = "Runs JUnit Jupiter tests."
useJUnitPlatform {
includeEngines "junit-jupiter"
excludeTags "failing-test-case"
}
filter {
includeTestsMatching "org.springframework.test.context.junit.jupiter.*"
}
reports.junitXml.destination = file("$buildDir/test-results")
// Java Util Logging for the JUnit Platform.
// systemProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager")
}
test {
description = "Runs JUnit 4 tests."
dependsOn testJUnitJupiter, testNG
useJUnit()
scanForTestClasses = false
include(["**/*Tests.class", "**/*Test.class"])
exclude(["**/testng/**/*.*", "**/jupiter/**/*.*"])
reports.junitXml.destination = file("$buildDir/test-results")
}
您当然可以根据需要命名和配置它们。
关于java - 如何在 Gradle 中运行 JUnit 5 和 JUnit 4 测试套件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54493166/