安卓+ JaCoCo:jacocoTestCoverageVerification 永远不会失败

发布于 2025-01-12 19:14:43 字数 2232 浏览 5 评论 0原文

我正在尝试配置一个新的 Android 项目以使用 JaCoCo 进行测试覆盖率,但当覆盖率低于某个阈值时会失败。我使用过 此文档,并查看了许多 SO 帖子。我预计任务会失败,因为我没有测试覆盖率,但它成功了,没有任何警告。

我的 :app build.gradle 文件包含以下内容:

plugins {
    ...
    id 'jacoco'
}

jacoco {
    toolVersion = "0.8.7"
    reportsDirectory = layout.buildDirectory.dir('app/build/reports')
}

task jacocoTestCoverageVerification(type: JacocoCoverageVerification, dependsOn: ['jacocoTestReport']) {
    onlyIf = { true }
    violationRules {
        failOnViolation = true
        rule {
            limit {
                minimum = 1.0
            }
        }
    }
}

task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
    finalizedBy jacocoTestCoverageVerification

    onlyIf = {true}
    reports {
        xml.enabled  true
        html.enabled  true
        html.outputLocation = layout.buildDirectory.dir('app/build/reports')
    }


}

tasks.withType(Test) {
    finalizedBy jacocoTestReport

    jacoco.includeNoLocationClasses = true
    jacoco.excludes = ['jdk.internal.*']
}

android {
    ...
    jacoco{
        version  = "0.8.7"
    }
}

当我尝试运行测试时,使用以下任何一项:

./gradlew test
./gradlew jacocoTestReport
./gradlew jacocoTestCoverageVerification

结果始终相同:

...

> Task :app:connectedDebugAndroidTest
Starting 2 tests on Nexus 5X - 8.1.0

> Task :app:createDebugAndroidTestCoverageReport
> Task :app:createDebugCoverageReport
> Task :app:jacocoTestReport
> Task :app:jacocoTestCoverageVerification

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 31s
93 actionable tasks: 93 executed

Build Analyzer results available
1:10:48 PM: Task execution finished 'test'.

我预计任务会失败,因为我没有测试覆盖率。我添加了一个实用程序类和方法,并部分实现了一个测试,以便 JaCoCo 报告显示一个类的覆盖率为 60%,但这并没有改变任何东西。

I am trying to configure a new Android project to use JaCoCo for test coverage and fail when coverage is below a certain threshold. I have used this documentation, as well as looking at many SO posts. I expect the task to fail, since I don't have test coverage, but it succeeds without any warnings.

My :app build.gradle file includes the following:

plugins {
    ...
    id 'jacoco'
}

jacoco {
    toolVersion = "0.8.7"
    reportsDirectory = layout.buildDirectory.dir('app/build/reports')
}

task jacocoTestCoverageVerification(type: JacocoCoverageVerification, dependsOn: ['jacocoTestReport']) {
    onlyIf = { true }
    violationRules {
        failOnViolation = true
        rule {
            limit {
                minimum = 1.0
            }
        }
    }
}

task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
    finalizedBy jacocoTestCoverageVerification

    onlyIf = {true}
    reports {
        xml.enabled  true
        html.enabled  true
        html.outputLocation = layout.buildDirectory.dir('app/build/reports')
    }


}

tasks.withType(Test) {
    finalizedBy jacocoTestReport

    jacoco.includeNoLocationClasses = true
    jacoco.excludes = ['jdk.internal.*']
}

android {
    ...
    jacoco{
        version  = "0.8.7"
    }
}

When I try to run tests, using any of the following:

./gradlew test
./gradlew jacocoTestReport
./gradlew jacocoTestCoverageVerification

The results are always the same:

...

> Task :app:connectedDebugAndroidTest
Starting 2 tests on Nexus 5X - 8.1.0

> Task :app:createDebugAndroidTestCoverageReport
> Task :app:createDebugCoverageReport
> Task :app:jacocoTestReport
> Task :app:jacocoTestCoverageVerification

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 31s
93 actionable tasks: 93 executed

Build Analyzer results available
1:10:48 PM: Task execution finished 'test'.

I expect the task to fail, since I have no test coverage. I have added a utility class and method, and partially implemented a test so that the JaCoCo report shows 60% coverage for one class, but this has not changed anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文