首页 文章

如何在Gradle中使用JUnit 5?

提问于
浏览
24

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

Expected result: JUnit 4测试在输出中给出了一个很好的'passed',在 build/reports/tests 中给出了一个html报告 .

Actual result: 下面的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 resultsbuild/test-results/test 中的xml相似,而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

而我唯一的测试包含

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现在想要源和测试在他们自己的包中 .

What I tried

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

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

回到上面的问题链接,并链接到这个Jetbrains blog,它使用与上述问题相同的行 . 还链接到:

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

这是在_1536962中解释的 . 好吧,当我运行它时,输出显示它找不到这个版本但是根据Maven Repository这个是针对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")

但结果没有改变 .

我的模板项目位于https://github.com/PHPirates/java-template-project,但此问题应包含所有必要信息 .

4 回答

  • 29

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

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

  • 4

    新增:Gradle 4.6中的JUnit 5支持

    从Gradle 4.6开始in this GitHub issue指出JUnit 5是支持的!官方发布的4.6版本(在编辑最新版本时,但请查看下面的链接)at docs.gradle.org . 旧设置仍然有效,但使用此设置可使构建文件更加清晰 .

    Update Gradle

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

    How to use the built-in JUnit 5

    然后使用java文件,目录结构等来自 build.gradle 文件的问题(使用新的 plugins 块)

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

    PS对于绝对最小版本,请参阅Ray's answer .

  • 7

    只是添加到知识库,我只需要使用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()
    }
    
  • 18

    由于github issue内置支持JUnit 5,计划用于Gradle 4.6

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

    预期结果:JUnit 4测试在输出中给出了一个很好的'传递',在构建/报告/测试中给出了一个html报告 .

    UPD:

    gradle 4.6-rc-1于2018年2月16日发布,此版本为junit 5提供内置支持 .

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

    gradle wrapper --gradle-version=4.6-rc-1
    

    并且只为build.gradle添加一行:

    test {
        useJUnitPlatform()
    }
    

相关问题