如何在Gradle中使用JUnit 5?

PHP*_*ate 33 junit gradle build.gradle junit5

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

预期的结果: JUnit 4测试在输出中给出了一个很好的'传递',并且输入了一个html报告build/reports/tests.

实际结果: JUnit 5测试如下所示不输出任何内容(...) build succesful,虽然我知道测试实际上没有运行,因为没有传递/跳过/失败的测试日志输出,并且fail在测试中放置一个使构建成功.

在很多我认为主要是不相关的输出之间运行gradle test --info收益率Skipping task ':testClasses' as it has no actions..令人惊讶的是,它也说Executing task ':test'Generating HTML test report... Finished generating test html resultsxml中的类似build/test-results/test,而xml没有生成,html显示没有测试运行且没有错误,测试确实没有运行.

我认为非常有趣的是gradle test --debug收益率

[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
Run Code Online (Sandbox Code Playgroud)

而我唯一的测试包含

fail("test fails");
Run Code Online (Sandbox Code Playgroud)

我认为这很奇怪!

我的构建文件是

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"
    }
}
Run Code Online (Sandbox Code Playgroud)

我的考试是

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

我的文件夹结构是,使用包mypackage,

java-template-project
--- src
    --- mypackage
        --- HelloWorld.java
--- test
    --- mypackage
        --- HelloWorldTest.java
Run Code Online (Sandbox Code Playgroud)

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

java-template-project
--- java-template-project_main
    --- src/mypackage
        --- HelloWorld(.java)
--- java-template-project_test
    --- test/mypackage
        --- HelloWorldTest(.java)
Run Code Online (Sandbox Code Playgroud)

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

我尝试了什么

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

PHP*_*ate 40

新增内容:Gradle 4.6中的JUnit 5支持

正如Gradle 4.6以后的GitHub问题所指出的那样,支持JUnit 5!官方发布说明4.6(在编辑最新版本时,但请查看下面的链接)在docs.gradle.org.旧设置仍然有效,但使用此设置可使构建文件更加清晰.

更新Gradle

首先,确保您使用的是最新的Gradle版本,请在GitHub版本中查看最新版本.如果是例如4.6,则在项目位置的终端中运行,build.gradle或确保在gradlew wrapper --gradle-version=4.6文件中更新此行:gradle/wrapper/gradle-wrapper.properties.

如何使用内置的JUnit 5

然后使用java文件,目录结构等来自distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip文件的问题(使用新build.gradle块)

plugins {
    id 'java'
}

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.0.3'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.0.3'
}

// These lines can be removed when you use the default directories src/main/kotlin and src/test/kotlin
sourceSets {
    main.java.srcDirs += 'src'
    main.resources.srcDirs += 'src'
    test.java.srcDirs += 'test'
    test.resources.srcDirs += 'test'
}

// Java target version
sourceCompatibility = 1.8

test {
    // Enable JUnit 5 (Gradle 4.6+).
    useJUnitPlatform()

    // Always run tests, even when nothing changed.
    dependsOn 'cleanTest'

    // Show test results.
    testLogging {
        events "passed", "skipped", "failed"
    }
}
Run Code Online (Sandbox Code Playgroud)

PS对于绝对最小版本,请参阅Ray的答案.


Ral*_*ert 22

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

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'
}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅JUnit doc表单.


Ray*_*yek 10

只是添加到知识库,我只需要使用gradle 4.7:

apply plugin: 'java'
repositories {
    jcenter()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.1.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.1.1'
}

test {
    useJUnitPlatform()
}
Run Code Online (Sandbox Code Playgroud)

  • 感谢您添加一个更简单的示例!我认为使用 `plugins` 块而不是 `apply plugin` 是[未来](/sf/answers/2264727111/),但目前并不重要。 (2认同)

fbo*_*kov 5

由于github 问题,JUnit 5的内置支持计划在Gradle 4.6中进行

因此从gradle 4.6开始,您的预期结果必须与实际结果相同。

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

UPD:

gradle 4.6-rc-1于2018年2月16日发布,该版本提供了对junit 5的内置支持。

要启用junit 5支持,您需要更新gradle包装器:

gradle wrapper --gradle-version=4.6-rc-1
Run Code Online (Sandbox Code Playgroud)

并仅添加一行到build.gradle:

test {
    useJUnitPlatform()
}
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

25554 次

最近记录:

6 年,10 月 前