简体   繁体   English

Jacoco Gradle 插件及其对测试任务的依赖

[英]Jacoco Gradle Plugin and its dependency on the test task

I'm following these docs我正在关注这些文档

https://docs.gradle.org/4.2.1/userguide/jacoco_plugin.html https://docs.gradle.org/4.2.1/userguide/jacoco_plugin.html

In a java app, with the java plugin enabled, I've added this line to enable the jacoco plugin在启用了 java 插件的 java 应用程序中,我添加了这一行来启用 jacoco 插件

apply plugin: 'jacoco'应用插件:'jacoco'

The docs state文档状态

If the Java plugin is also applied to your project, a new task named jacocoTestReport is created that depends on the test task如果 Java 插件也应用到你的项目中,则会创建一个名为 jacocoTestReport 的新任务,该任务依赖于测试任务

So now when I run所以现在当我跑

./gradlew build jacocoTestReport

I can see it kicking off the tests as part of build, but we have some failing tests, so the whole test task reports as failed.我可以看到它作为构建的一部分启动了测试,但是我们有一些失败的测试,所以整个测试任务报告为失败。

The jacoco code coverage report doesn't generate anything. jacoco 代码覆盖率报告不会生成任何内容。

If I run如果我跑

./gradlew jacocoTestReport

I get a successful report我得到一个成功的报告

This may sound like a daft question, but is the reason the first command, ./gradle1 build jacocoTestReport doesn't generate the report, is because of the failing tests?这听起来像是一个愚蠢的问题,但第一个命令./gradle1 build jacocoTestReport不生成报告的原因是因为测试失败吗?

Would just ./gradlew build run the jacocoTestReport task if the tests passed?如果测试通过, ./gradlew build运行jacocoTestReport任务吗?

I prefer to "teach a man to fish" rather than give a fish.我更喜欢“授人以渔”而不是授人以渔。 In that spirit I suggest you add the task tree plugin so you can see a visual representation of what's going on for yourself本着这种精神,我建议您添加任务树插件,以便您可以直观地看到自己正在发生的事情

plugins {
  id "com.dorongold.task-tree" version "1.3.1"
}

You can then try然后你可以试试

gradle build taskTree

and

gradle jacocoTestReport taskTree 

And see what's in the task tree for each并查看每个任务树中的内容

I've commented out the failing tests and ran ./gradlew build我已经注释掉了失败的测试并运行了 ./gradlew build

The remaining tests passed, but no report.其余测试通过,但没有报告。

I will have a bash at lance-java's tip for the top, as I do like fishing!我将在 lance-java 的顶端猛击一下,因为我喜欢钓鱼!

Also going to try ./gradlew build jacocoTestReport也要去尝试 ./gradlew build jacocoTestReport

./gradlew build jacocoTestReport is the winner!! ./gradlew build jacocoTestReport 是赢家!!

It is because the tests failed.那是因为测试失败了。

Adding the following will to build.gradle will ensure that coverage is generated regardless:将以下 will 添加到 build.gradle 将确保无论生成覆盖率:

test.finalizedBy jacocoTestReport

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

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