Gradle:如何使用jacoco生成集成测试的覆盖率报告 [英] Gradle : How to generate coverage report for Integration test using jacoco

查看:2456
本文介绍了Gradle:如何使用jacoco生成集成测试的覆盖率报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对gradle很陌生。我正在使用下面的代码。但它产生了单元测试用例的覆盖范围。但它并未产生集成测试用例。

  test {
dependsOn jettyRunWar
我的测试类位于src / test / java包中。 ignoreFailures true
finalizedBy jettyStop
}

apply plugin:'jacoco'

jacocoTestReport {
group =Reporting
description =运行测试后生成Jacoco覆盖报告。
additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
}


解决方案

看起来像,你需要告诉build.gradle是你的集成测试(即包含那些IT测试的文件夹)在哪里使用sourceSets。在我的情况下,我有src / java(而不是src / main / java - gradle默认)下的源代码..我的单元测试(Junit)在测试/ java文件夹下,以及我的集成测试在src / java-test文件夹下。 / p>

  sourceSets {
main {
java {
srcDir'src / java'


test {
java {
srcDir'test / java'
}
资源{
srcDir'test / resources'
srcDir'conf'
}
}
integrationTest {
java {
srcDir'src / java-test'
}
}
}

然后,我将integrationTest任务设置为......您可以调整因为你可能没有cleanTest(我创建的自定义任务),所以你可以忽略dependsOn ...我认为在你的情况下,你会使用类似jettyStart的东西,因为你正在使用IT测试(启动容器运行IT测试,然后最终确定如果没有,那么可以使用jetty插件)
$ b $ pre $ task integrationTest(type:Test,dependsOn:cleanTest){
jacoco {
// destinationFile = file($ buildDir / jacoco / jacocoTest.exec)
destinationFile = file($ buildDir / jacoco / integrationTest.exec)
// classDumpFile = file ($ buildDir / jacoco / classpathdumps)
classDumpFile = file($ buildDir / classes / integrationTest)
}
testClassesDir = sourceSets.integrationTest.output.classesDir $ b $ classpath = sourceSets.integrationTest.runtimeClasspath
}

查看这篇文章以获取更详细的输出结构和脚本我有我的结局。我得到.exec单元测试(test.exec)和IT测试intergrationTest.exec ..但我没有得到两个测试jacoco.xml / jacocoHtml报告。我还发现,如果我运行gradle clean build(其中包括调用test任务)和gradle clean build integrationTest,那么稍后将覆盖构建/测试结果文件夹中的单元测试数据以及构建/报告/测试文件夹。



Jacoco Unit and Integration Tests coverage - individual and overall



注意:在我的情况下,jacocoTestReport是在全局gradle init.d文件夹中的一个普通Gradle文件。这将帮助我们不要在项目级别的build.gradle文件中包含相同的代码。


I am new to gradle. I am using the below code. But it generates coverage for unit test cases. But it didn't generate for integration test cases. I have my test classes in the package src/test/java.

test {
    dependsOn jettyRunWar
    ignoreFailures true
    finalizedBy jettyStop
}

apply plugin: 'jacoco'

jacocoTestReport {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
}

解决方案

It seems like, what you need to tell build.gradle is where are your Intergration tests (i.e. folder containing those IT tests) using sourceSets. In my case, i have source under src/java (instead of src/main/java - gradle default).. my unit tests (Junit) under test/java folder, and my integration tests under src/java-test folder.

sourceSets {
   main {
      java {
         srcDir 'src/java'
      }
   }
   test {
      java {
         srcDir 'test/java'
      }
      resources {
         srcDir 'test/resources'
         srcDir 'conf'
      }
   }
   integrationTest {
      java {
         srcDir 'src/java-test'
      }
   }
}

Then, I have integrationTest task as ... which you can tweak as you might not have cleanTest (custom task that i have created), so you can ignore that dependsOn... i think in your case you'll use something like jettyStart as you're using that for IT tests (starting the container for running IT tests and then finalizedBy feature to stop jetty .. jetty plugin)

task integrationTest( type: Test, dependsOn: cleanTest ) {
   jacoco {
      //destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
      destinationFile = file("$buildDir/jacoco/integrationTest.exec")
      //classDumpFile = file("$buildDir/jacoco/classpathdumps")
      classDumpFile = file("$buildDir/classes/integrationTest")
   }
   testClassesDir = sourceSets.integrationTest.output.classesDir
   classpath = sourceSets.integrationTest.runtimeClasspath
}

SEE this post for more detailed output structure and script that I have at my end. Im getting the .exec for both Unit tests (test.exec) and IT tests intergrationTest.exec.. but Im not getting the jacoco.xml/jacocoHtml reports for both tests. I also found that, if I run "gradle clean build" (which includes call to "test" task) and "gradle clean build integrationTest" then, later one overwrites unit tests data in build/test-results folder and build/reports/tests folder.

Jacoco Unit and Integration Tests coverage - individual and overall

NOTE: in my case, jacocoTestReport is defined in the global gradle init.d folder in one of the common gradle file. This will help us not to include the same code in all / at project level build.gradle file.

这篇关于Gradle:如何使用jacoco生成集成测试的覆盖率报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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