如何在Gradle中使用JUnit 5? [英] How to use JUnit 5 with Gradle?

查看:1551
本文介绍了如何在Gradle中使用JUnit 5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



预期结果: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>



显然这不是关于这个话题的第一个问题,我发现的所有相关问题都是


解决方案

您需要两种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'
}

请参阅 JUnit doc 可以获得更多的信息。


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

解决方案

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆