首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gradle Jacoco和JUnit5

Gradle Jacoco和JUnit5
EN

Stack Overflow用户
提问于 2016-09-07 14:48:06
回答 4查看 5.9K关注 0票数 19

我们刚刚将我们的单元测试移植到了JUnit5。意识到这仍然是相当早的采用,在google上几乎没有任何提示。

最具挑战性的是为我们在jenkins上使用的Junit5测试获得jacoco代码覆盖率。因为我花了差不多一天的时间才弄明白,所以我想我可以分享一下。不过,如果您知道更好的解决方案,我会很感兴趣的!

代码语言:javascript
复制
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'

那么问题似乎是,在org.junit.platform.gradle.plugin中定义的junitPlatformTest在gradle生命周期阶段定义得太晚了,因此在解析脚本时是未知的。

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

代码语言:javascript
复制
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
            })
    }
}

最后,有必要配置junitPlatform插件。下面的代码允许运行junit 5标签的命令行配置:你可以通过运行以下命令来运行所有带有'unit‘标签的测试:

代码语言:javascript
复制
gradle clean junitPlatformTest -PincludeTags=unit

您可以使用以下命令运行所有缺少unit和integ标记的测试

代码语言:javascript
复制
gradle clean junitPlatformTest -PexcludeTags=unit,integ

如果未提供任何标记,则将运行所有测试(默认)。

代码语言:javascript
复制
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
}
EN

回答 4

Stack Overflow用户

发布于 2016-09-08 16:58:13

谢谢,所以黑客现在看起来是这样的:

代码语言:javascript
复制
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
            })
}
票数 5
EN

Stack Overflow用户

发布于 2017-07-04 21:54:33

也可以通过直接代理注入来解决:

代码语言:javascript
复制
subprojects {
   apply plugin: 'jacoco'

   jacoco {
        toolVersion = "0.7.9"
   }

   configurations {
        testAgent {
            transitive = false
        }
   }

   dependencies {
        testAgent("org.jacoco:org.jacoco.agent:0.7.9:runtime")
   }

   tasks.withType(JavaExec) {
        if (it.name == 'junitPlatformTest') {
            doFirst {
                jvmArgs "-javaagent:${configurations.testAgent.singleFile}=destfile=${project.buildDir.name}/jacoco/test.exec"
            }
        }
    }
}

然后,报告将随jacocoTestReport任务一起提供

票数 4
EN

Stack Overflow用户

发布于 2016-09-07 20:22:35

要获得对junitPlatformTest任务的引用,另一种选择是在项目中实现一个afterEvaluate块,如下所示:

代码语言:javascript
复制
afterEvaluate {
  def junitPlatformTestTask = tasks.getByName('junitPlatformTest')

  // do something with the junitPlatformTestTask
}

有关更多示例,请参阅我在GitHub for JUnit 5上的评论。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39362955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档