2016-09-07 84 views
17

我们仅将单元测试移植到JUnit5。意识到这仍然是相当早的采用与谷歌的一点提示。Gradle Jacoco和JUnit5

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

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' 

那么问题似乎是,junitPlatformTest作为org.junit.platform.gradle.plugin定义是指在gradle这个生命周期阶段也 晚,所以当脚本解析未知。

下面的技巧是必要的,以便仍然能够确定哪些观察junitPlatformTest任务jacoco任务。

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个标签应运行: 可以通过运行运行与“单位”标签的所有测试:

gradle clean junitPlatformTest -PincludeTags=unit 

您可以运行它缺少两个单元和INTEG标记所有测试使用

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 
} 
+0

我投票,因为它是相当的解决方案,然后一个问题 – Stanislav

+1

如果这是分成的问题,回答这将是很好,关闭这一问题作为题外话。 –

+0

我同意@PaulHicks这应该是一个自我回答的问题。你可以在这里看到文档(https://stackoverflow.com/help/self-answer)。此外,“Gradle生命周期中太迟”问题应该在[M5]中修复(https://github.com/junit-team/junit5/issues/708)。 – mkobit

回答

1

要到junitPlatformTest任务的引用,另一种选择就是在这样的项目实施afterEvaluate块:

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

    // do something with the junitPlatformTestTask 
} 

查看GitHub for JUnit 5我的意见作进一步的例子。

5

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

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

也可以解决了与直接剂注射:

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任务

+0

谢谢伊恩。它为我工作 – Sean

-2

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

@RunWith(JUnitPlatform.class) 
public class ClassTest { 
} 
相关问题