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

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

问题描述

在成功运行 JUnit 4 测试后,我尝试将 JUnit 5 与 Gradle 结合使用.

I am trying to use JUnit 5 with Gradle after I succeeded in running a JUnit 4 test.

预期结果: JUnit 4 测试在输出中给出了一个很好的通过",并在 build/reports/tests 中给出了一个 html 报告.

Expected result: Tthe JUnit 4 test gave a nice 'passed' in the output and an html report in build/reports/tests.

实际结果:下面的 JUnit 5 测试除了 (...) build succesful 之外没有任何输出,虽然我知道测试实际上并没有运行,因为那里没有测试日志输出通过/跳过/失败,并且在测试中放置 fail 可以保持构建成功.

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.

运行 gradle test --info 产生 Skipping task ':testClasses' 因为它没有任何动作. 在我认为大部分是不相关的输出中.令人惊讶的是,它还说 Executing task ':test'Generating HTML test report... Finished generated test html resultsbuild/test 中的 xml 类似-results/test,虽然没有生成xml,但是html显示没有测试运行也没有错误,测试确实没有运行.

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.

我也觉得很有趣的是,gradle test --debug 产生了

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

虽然我唯一的测试包含

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

My folder structure is, using package mypackage,

java-template-project
--- src
    --- mypackage
        --- HelloWorld.java
--- test
    --- mypackage
        --- HelloWorldTest.java

在我使用的 IntelliJ 2017.1.3 中,模块结构如下

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)

因为现在 Gradle 想要源代码和测试在他们自己的包中.

because Gradle nowadays wants the source and tests in their own package.

我尝试了什么

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

Obviously this is not the first question about this topic, all the relevant questions I found are

但是正如您所看到的,这是针对旧版本的 IntelliJ,根据其中一个答案,我已经在使用 IJ 2016.3.3 及更高版本的语法,在一个 JUnit 依赖项行中,所以应该没问题.

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.

从 JUnit 4 升级到带有 gradle 的 intellij 中的 JUnit 5

返回上述问题的链接,以及指向此Jetbrains 博客 使用与上述问题相同的行.还链接到:

Links back to above question, and links to this Jetbrains blog which uses the same line as above question. Also links to:

将 JUnit 5 测试结果与 Intellij 测试集成报告这个在问题中也显示为依赖

Integrate JUnit 5 tests results with Intellij test report This one shows, in the question, as dependency also

testRuntime("org.junit.vintage:junit-vintage-engine:5.0.0-M1")

为什么在 IntelliJ 中运行 TestCase 时 JUnit Jupiter 和 JUnit Vintage 分开了?好吧,当我运行它时,输出显示它找不到这个版本,但根据 Maven Repository 这个是针对 JUnit 5 的:

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")

那里的答案指出,您可以在 IntelliJ 中运行测试,因为更高版本支持 JUnit 5.我知道,当我在 IntelliJ 中运行时,测试运行良好.但我想使用 Gradle(和需要依赖管理的 Travis).

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).

如何捕获标准输出/stderr 在 junit 5 gradle 测试报告中?

我尝试使用

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-项目,但这个问题应该包含所有必要的信息.

My template project is located on https://github.com/PHPirates/java-template-project but this question should contain all information necessary.

推荐答案

您需要两个 JUnit 版本的引擎,并且您需要应用 JUnit 平台 gradle 插件.我在你的 gradle 文件中没有看到.这是一个同时执行 JUnit 4 和 5 的工作 gradle 构建:

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'
}

请参阅 JUnit 文档 表单更多信息.

See the JUnit doc form more information on that.

这篇关于如何在 Gradle 中使用 JUnit 5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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