在成功运行 JUnit 4 测试后,我尝试将 JUnit 5 与 Gradle 一起使用。
预期结果:JUnit 4 测试在输出中给出了很好的“通过”结果,并在build/reports/tests
.
实际结果:下面的 JUnit 5 测试不输出任何内容(...) build successful
,虽然我知道测试实际上并未运行,因为没有测试日志输出通过/跳过/失败,并放置fail
在测试中保持构建成功。
Running gradle test --info
yields Skipping task ':testClasses' as it has no actions.
其中很多我认为大多是不相关的输出。
令人惊讶的是,它还说Executing task ':test'
and Generating HTML test report... Finished generating test html results
和 xml 类似build/test-results/test
,虽然没有生成xml,但html显示没有测试运行,也没有错误,并且测试确实没有运行。
我也觉得很有趣的是gradle test --debug
yields
[TestEventLogger] Gradle Test Run :test STARTED
[org.gradle.api.internal.tasks.testing.junit.JUnitDetector] test-class-
scan : failed to scan parent class java/lang/Object, could not find the class file
[TestEventLogger]
[TestEventLogger] Gradle Test Run :test PASSED
虽然我唯一的测试包含
fail("test fails");
我觉得这很奇怪!
我的构建文件是
apply plugin: 'java'
test {
dependsOn 'cleanTest' // run tests every time
}
sourceSets {
main {
java {
srcDirs 'src'
}
}
test {
java {
srcDirs 'test'
}
}
}
repositories {
mavenCentral()
}
dependencies {
// when using this, it worked with a junit 4 test
// testCompile 'junit:junit:4.10'
// this should be needed for junit 5 (using M4 is required since IJ 2017.1.2
testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M4")
}
test {
testLogging {
events "passed", "skipped", "failed"
}
}
我的测试是
package mypackage;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class HelloWorldTest {
@Test
public void testHelloWorld(){
assertEquals(2, 1+1, "message");
}
}
我的文件夹结构是,使用包mypackage
,
java-template-project
--- src
--- mypackage
--- HelloWorld.java
--- test
--- mypackage
--- HelloWorldTest.java
在我正在使用的 IntelliJ 2017.1.3 中,模块结构如下所示
java-template-project
--- java-template-project_main
--- src/mypackage
--- HelloWorld(.java)
--- java-template-project_test
--- test/mypackage
--- HelloWorldTest(.java)
因为 Gradle 现在希望源代码和测试放在自己的包中。
我尝试过的
显然这不是关于这个话题的第一个问题,我发现的所有相关问题都是
-
在 IntelliJ 中运行 jUnit 5 测试的 Gradle 项目 https://stackoverflow.com/questions/38293901/gradle-project-running-junit-5-tests-in-intellij
但正如您所看到的,这是针对旧版本的 IntelliJ,并且我已经根据一个 JUnit 依赖行中的答案之一使用 IJ 2016.3.3 及更高版本的语法,因此应该没问题。
-
使用 gradle 在 intellij 中从 JUnit 4 升级到 JUnit 5 https://stackoverflow.com/questions/38576011/upgrade-from-junit-4-to-junit-5-in-intellij-with-gradle
链接回上述问题,并链接到此Jetbrains 博客 https://blog.jetbrains.com/idea/2016/08/using-junit-5-in-intellij-idea/它使用与上面问题相同的行。还链接到:
-
将 JUnit 5 测试结果与 Intellij 测试报告集成 https://stackoverflow.com/questions/38576108/integrate-junit-5-tests-results-with-intellij-test-report在问题中,这也显示为依赖性
testRuntime("org.junit.vintage:junit-vintage-engine:5.0.0-M1")
这是在中解释的当我在 IntelliJ 中运行 TestCase 时,为什么 JUnit Jupiter 和 JUnit Vintage 是分开的? https://stackoverflow.com/questions/38822189/why-were-junit-jupiter-and-junit-vintage-separated-when-i-running-testcase-in-in好吧,当我运行它时,输出显示它找不到这个版本,但是根据Maven 存储库 https://mvnrepository.com/artifact/org.junit.vintage/junit-vintage-engine/4.12.0-M4这是针对 JUnit 5 的:
testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M4")
那里的答案指出,您可以只在 IntelliJ 中运行测试,因为更高版本有 JUnit 5 支持。我知道,当我从 IntelliJ 中运行时,测试运行良好。但我想使用 Gradle(和 Travis,需要依赖管理)。
-
如何在junit 5 gradle测试报告中捕获stdout/stderr? https://stackoverflow.com/questions/43151448/how-to-capture-stdout-stderr-in-junit-5-gradle-test-report
我尝试使用
testCompile("org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3")
testCompile("org.junit.jupiter:junit-jupiter-engine:5.0.0-M3")
但结果没有改变。
我的模板项目位于https://github.com/PHPirates/java-template-project https://github.com/PHPirates/java-template-project但这个问题应该包含所有必要的信息。