问题描述
预期结果:JUnit 4中,我使用JUnit 5和Gradle进行了一次JUnit 4测试。测试在输出中给出了一个很好的通过,并在 build / reports / tests
中给出了一个html报告。 strong>实际结果:下面的JUnit 5测试不会输出除(...)构建成功
之外的任何内容,而我知道测试并未实际运行因为没有测试日志输出通过/跳过/失败,并且在测试中放置失败
可以使构建成功。
运行 gradle test --info
产生跳过任务':testClasses',因为它没有任何动作。
很多我认为大多数不相关的输出。
令人惊讶的是,它还表示执行任务':test'
和生成HTML测试报告...完成生成测试html结果
以及类似于 build / test-results / test
中的xml,而未生成xml,则html显示没有运行测试并且没有错误,并且测试确实没有运行。
我也觉得很有趣,是 gradle test --debug
产生
[TestEventLogger] Gradle测试运行:test STARTED
[org.gradle.api.internal.tasks.testing.junit .JUnitDetector] test-class-
扫描:无法扫描父类java / lang / Object,找不到类文件
[TestEventLogger]
[TestEventLogger] Gradle测试运行:test PASSED
而我唯一的测试包含
失败(测试失败);
我认为这很奇怪!
我的构建文件是
apply plugin:'java'
test {
dependsOn 'cleanTest'//每次运行测试
}
sourceSets {
main {
java {
srcDirs'src'
test {
java {
srcDirs'test'
}
}
}
存储库{
mavenCentral()
}
依赖关系{
//使用它时,它使用junit 4 test
// testCompile' junit:junit:4.10'
//这是junit 5需要的(自从IJ 2017.1.2
testCompile(org.junit.jupiter:junit-jupiter-api:5.0。 0-M4)
}
测试{
testLogging {
事件通过,跳过,失败
}
}
我的测试是
p ackage 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
---测试
--- 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现在希望在他们自己的包中使用源代码和测试。
/ strong>
显然这不是关于这个话题的第一个问题,我发现的所有相关问题都是
-
,它使用与上面的问题。也链接到:
-
这个在问题中显示为依赖关系也是
testRuntime(org.junit.vintage:junit-vintage-engine:5.0.0-M1)
解释在
当我运行它时,输出显示它找不到版本,但根据这个是针对JUnit 5的:
testRuntime(org.junit.vintage:junit-vintage-engine:4.12.0- M4)
这里的答案指出,您可以在IntelliJ中运行测试,因为更高版本具有JUnit 5支持。我知道,当我从IntelliJ内运行时,测试运行良好。但我想使用Gradle(和Travis,它需要依赖管理)。
我试过使用
testCompile(org.junit.platform:junit-platform-gradle-plugin: 1.0.0-M3)
testCompile(org.junit.jupiter:junit-jupiter-engine:5.0.0-M3)
但结果没有变化。
我的模板项目位于,但这个问题应该包含所有必要的信息。
解决方案您需要两种JUnit版本的引擎,并且您需要应用JUnit平台Gradle插入。我没有在你的gradle文件中看到。这是执行JUnit 4和5的工作gradle构建:
buildscript {
repositories {
mavenCentral ()
}
依赖关系{
classpath(org.junit.platform:junit-platform-gradle-plugin:1.0.0-M4)
}
}
apply plugin:'org.junit.platform.gradle.plugin'
...
依赖关系{
...
testCompile(junit:junit:4.12)
testRuntime(org.junit.vintage:junit-vintage-engine:4.12.0-M4)
testCompile(org。 junit.jupiter:junit-jupiter-api:5.0.0-M4)
testRuntime(org.junit.jupiter:junit-jupiter-engine:5.0.0-M4)
//在IDE中使用JUnitPlatform Runner
testCompile(org.junit.platform:junit-platform-runner:1.0.0-M4)
}
junitPlatform {
details'tree'
}
请参阅可以获得更多的信息。
I am trying to use JUnit 5 with Gradle after I succeeded in running a JUnit 4 test.
Expected result: Tthe JUnit 4 test gave a nice 'passed' in the output and an html report in build/reports/tests
.
Actual result: The JUnit 5 test as below does not output anything besides (...) build succesful
, while I know the test is not actually run since there is no test log output passed/skipped/failed, and putting a fail
in the test keeps the build successful.
Running gradle test --info
yields Skipping task ':testClasses' as it has no actions.
among a lot of I think mostly unrelevant output.Surprisingly, it also says Executing task ':test'
and Generating HTML test report... Finished generating test html results
and similar for the xml in build/test-results/test
, while the xml is not generated, the html shows no tests run and no errors, and the test is indeed not run.
What I also think very interesting, is that 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
while my only test contains
fail("test fails");
which I think is very strange!
My build file is
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"
}
}
My test is
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");
}
}
My folder structure is, using package mypackage
,
java-template-project
--- src
--- mypackage
--- HelloWorld.java
--- test
--- mypackage
--- HelloWorldTest.java
and in IntelliJ 2017.1.3, which I am using, the module structure looks like this
java-template-project
--- java-template-project_main
--- src/mypackage
--- HelloWorld(.java)
--- java-template-project_test
--- test/mypackage
--- HelloWorldTest(.java)
because Gradle nowadays wants the source and tests in their own package.
What I tried
Obviously this is not the first question about this topic, all the relevant questions I found are
Gradle project running jUnit 5 tests in IntelliJ
But as you can see this is for older versions of IntelliJ, and I am already using the syntax for IJ 2016.3.3 and higher according to one of the answers, in in the one JUnit dependency line, so that should be okay.
Upgrade from JUnit 4 to JUnit 5 in intellij with gradle
Links back to above question, and links to this Jetbrains blog which uses the same line as above question. Also links to:
Integrate JUnit 5 tests results with Intellij test reportThis one shows, in the question, as dependency also
testRuntime("org.junit.vintage:junit-vintage-engine:5.0.0-M1")
which is explained in Why were JUnit Jupiter and JUnit Vintage separated When I Running TestCase in IntelliJ?Well, when I ran it, the output showed it couldn't find this version but according to the Maven Repository this one is for JUnit 5:
testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M4")
The answers there note that you can just run the tests within IntelliJ since the later versions have JUnit 5 support. I know, and the test runs fine when I run from within IntelliJ. But I want to use Gradle (and Travis, which needs dependency management).
How to capture stdout/stderr in junit 5 gradle test report?
I tried using
testCompile("org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3")
testCompile("org.junit.jupiter:junit-jupiter-engine:5.0.0-M3")
but results didn't change.
My template project is located on https://github.com/PHPirates/java-template-project but this question should contain all information necessary.
解决方案 You need the engines for both JUnit versions, and you need to apply the JUnit platform gradle plugin. I do not see that in your gradle file. Here is a working gradle build executing both JUnit 4 and 5:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath ("org.junit.platform:junit-platform-gradle-plugin:1.0.0-M4")
}
}
apply plugin: 'org.junit.platform.gradle.plugin'
...
dependencies {
...
testCompile("junit:junit:4.12")
testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M4")
testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M4")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0-M4")
// Enable use of the JUnitPlatform Runner within the IDE
testCompile("org.junit.platform:junit-platform-runner:1.0.0-M4")
}
junitPlatform {
details 'tree'
}
See the JUnit doc form more information on that.
这篇关于如何在Gradle中使用JUnit 5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!