繁体   English   中英

Gradle Jacoco和JUnit5

[英]Gradle Jacoco and JUnit5

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

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

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任务,需要以下hack。

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'标记的所有测试:

gradle clean junitPlatformTest -PincludeTags=unit

您可以运行所有缺少单位和整数标记的测试

gradle clean junitPlatformTest -PexcludeTags=unit,integ

如果没有提供标签,则将运行所有测试(默认)。

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
}

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

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

也可以通过直接药剂注射来解决:

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任务一起提供

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

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

  // do something with the junitPlatformTestTask
}

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

您只需要将@RunWith(JUnitPlatform.class)添加到您的包中

@RunWith(JUnitPlatform.class)
public class ClassTest {
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM