Gradle Jacoco和JUnit5 [英] Gradle Jacoco and JUnit5

查看:259
本文介绍了Gradle Jacoco和JUnit5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将单元测试移植到了JUnit5。意识到这仍然是相当早的采用,在谷歌的小提示。



最具挑战性的是让jacoco代码覆盖我们在jenkins上使用的Junit5测试。由于这花了我近一天的时间才弄清楚,我以为我分享了。然而,如果你知道一个更好的解决方案,我会有兴趣知道!

buildscript {

依赖项{
//运行junit所需的依赖项5测试$ b $ classpath'org.junit.platform:junit-platform-gradle-plugin: 1.0.0-M2'
}
}

//包括jacoco插件
插件{
id'jacoco'
}

依赖关系{
testCompileorg.junit.jupiter:junit-jupiter-api:5.0.0-M2
runtimeorg.junit.jupiter:junit-jupiter-engine :5.0.0-M2
runtimeorg.junit.vintage:junit-vintage-engine:4.12.0-M2


apply plugin:'org。 junit.platform.gradle.plugin'

然后这个问题似乎是在org中定义的junitPlatformTest .junit.platform.gradle.plugin在gradle生命周期阶段被定义为
,因此在解析脚本时未知。



为了仍然能够定义观察junitPlatformTest任务的jacoco任务,需要进行以下hack。 $ b

  tasks.whenTaskAdded {task  - > 
if(task.name.equals('junitPlatformTest')){
System.out.println(ADDING TASK+ task.getName()+to the project!)

//配置jacoco来分析junitPlatformTest任务
jacoco {
//这个工具版本与
兼容toolVersion =0.7.6.201602180812
applyTo任务


//创建junit平台jacoco任务
project.task(类型:JacocoReport,junitPlatformJacocoReport,
{
sourceDirectories = files(./ src / main)
classDirectories = files($ buildDir / classes / main)
executionData task
})
}
}
code>

最后,需要配置junitPlatform插件。以下代码允许运行junit 5标签的命令行配置:
您可以运行以下代码运行所有带有'unit'标签的测试:

  gradle clean junitPlatformTest -PincludeTags =单元

您可以使用

运行所有缺少单元和集成标记的测试。gradle clean junitPlatformTest -PexcludeTags = unit,integ

如果没有提供标签,所有测试都会运行(默认)。 >

junitPlatform {

engines {
include'junit-jupiter'
包括'junit-vintage'
}

reportsDir =文件($ buildDir / test-results)

标签{
if(project.hasProperty('includeTags')){
for(String t:includeTags.split(',')){
(字符串t:excludeTags.split(','))包含t



if(project.hasProperty('excludeTags')){
{
exclude t
}
}
}

enableStandardTestTask false
}

解决方案

谢谢你,现在看起来像这样:

  project.afterEvaluate {
def junitPlatformTestTask = project.tasks.getByName('junitPlatformTest')

//配置jacoco来分析junitPlatformTest任务
jacoco {
//此工具版本与
兼容toolVersion =0.7.6.201602180812
applyTo junitPlatformTestTask
}

//创建junit平台jacoco任务
project.task(类型:JacocoReport,junitPlatformJacocoReport,
{
sourceDirectories = files(./ src / ma in)
classDirectories = files($ buildDir / classes / main)
executionData junitPlatformTestTask
})
}


We just ported our unit tests to JUnit5. Realizing that this is still rather early adoption with little hints on google.

The most challenging was to get jacoco code coverage for the Junit5 tests which we use on jenkins. Since this took me almost a day to figure out, I thought I share. Nevertheless, if you know of a better solution I would be interested to know!

buildscript {

    dependencies {
       // dependency needed to run junit 5 tests
       classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M2'
   }
}

// include the jacoco plugin
plugins {
    id 'jacoco'
}

dependencies {
    testCompile "org.junit.jupiter:junit-jupiter-api:5.0.0-M2"
    runtime "org.junit.jupiter:junit-jupiter-engine:5.0.0-M2"
    runtime "org.junit.vintage:junit-vintage-engine:4.12.0-M2"
}

apply plugin: 'org.junit.platform.gradle.plugin'

Then the problem seems to be that junitPlatformTest as defined in the org.junit.platform.gradle.plugin is defined too late in the gradle lifecycle phase and hence is unknown when the script is parsed.

The following hack is needed in order to still be able to define a jacoco task which observes the junitPlatformTest task.

tasks.whenTaskAdded { task ->
    if (task.name.equals('junitPlatformTest')) {
        System.out.println("ADDING TASK " + task.getName() + " to the project!")

    // configure jacoco to analyze the junitPlatformTest task
    jacoco {
        // this tool version is compatible with
        toolVersion = "0.7.6.201602180812"
        applyTo task
    }

    // create junit platform jacoco task
    project.task(type: JacocoReport, "junitPlatformJacocoReport",
            {
                sourceDirectories = files("./src/main")
                classDirectories = files("$buildDir/classes/main")
                executionData task
            })
    }
}

Finally it is necessary to configure the junitPlatform plugin. The following code allows command line configuration of which junit 5 tags shall be run: You can run all tests with 'unit' tag by running:

gradle clean junitPlatformTest -PincludeTags=unit

You can run all tests which are missing both unit and integ tag using

gradle clean junitPlatformTest -PexcludeTags=unit,integ

If no tags are provided all tests will be run (default).

junitPlatform {

    engines {
        include 'junit-jupiter'
        include 'junit-vintage'
    }

    reportsDir = file("$buildDir/test-results")

    tags {
        if (project.hasProperty('includeTags')) {
            for (String t : includeTags.split(',')) {
                include t
            }
        }

        if (project.hasProperty('excludeTags')) {
            for (String t : excludeTags.split(',')) {
                exclude t
            }
        }
    }

    enableStandardTestTask false
}

解决方案

Thank you, so the hack now looks like this:

project.afterEvaluate {
    def junitPlatformTestTask = project.tasks.getByName('junitPlatformTest')

    // configure jacoco to analyze the junitPlatformTest task
    jacoco {
        // this tool version is compatible with
        toolVersion = "0.7.6.201602180812"
        applyTo junitPlatformTestTask
    }

    // create junit platform jacoco task
    project.task(type: JacocoReport, "junitPlatformJacocoReport",
            {
                sourceDirectories = files("./src/main")
                classDirectories = files("$buildDir/classes/main")
                executionData junitPlatformTestTask
            })
}

这篇关于Gradle Jacoco和JUnit5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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