【问题标题】:Jacoco data file not readable for Android on TeamCityTeamCity 上的 Android 无法读取 Jacoco 数据文件
【发布时间】:2015-04-07 05:45:47
【问题描述】:

我今天正在学习 TeamCity 集成服务器,我正在尝试使用我的基于 Android Gradle 的应用程序启用 Jococo Reports。

This 文档向我展示了如何启用 Jococo 覆盖,并带有以下警告:

确保您的测试在 fork=true 模式下运行。否则可能无法正确收集覆盖数据。

我不知道该怎么做才能“在 fork=true 模式下运行我的测试”。 TeamCity 没有生成覆盖率报告,并通过以下日志警告我:

Jacoco 数据文件路径指定为 C:\TeamCity\buildAgent\temp\buildTmp\JACOCO5884661263301729570coverage\jacoco.exec 但不可读。不会收集覆盖范围。

我认为此警告与未在 fork=true 模式下运行测试有关。

所以,我的问题是:

  1. fork=true 模式的含义和
  2. 如何在 gradle 中启用它

谢谢!!!

【问题讨论】:

  • 我们改变了误导性的“fork=true”声明。感谢您指出。

标签: android gradle teamcity android-gradle-plugin


【解决方案1】:

经过一些研究,我能够指导 Teamcity 使用“服务消息”技术处理由 jacoco 生成的覆盖率报告,在 this 上进行了解释:

自 TeamCity 9.0 起,TeamCity 能够解析 JaCoCo 覆盖数据并使用以下格式的服务消息生成报告:

##teamcity[jacocoReport dataPath='<path to jacoco.exec file>']

所以,我修改了我的 build.gradle 文件,将以下几行添加到 jacocoTestReport 部分:

if (project.hasProperty("teamcity")) {
    println '##teamcity[jacocoReport dataPath=\'app/build/jacoco/testDebug.exec\' includes=\'com.mynamespace.myproject.*\' excludes=\'**/R.class **/R$*.class **/*$ViewInjector*.* **/BuildConfig.* **/Manifest*.*\']'
}

在那之后,完整的jacocoTestReport 是:

task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
    group = "Reporting"

    description = "Generate Jacoco coverage reports"

    classDirectories = fileTree(
            dir: '../app/build/intermediates/classes/debug',
            excludes: ['**/R.class',
                       '**/R$*.class',
                       '**/*$ViewInjector*.*',
                       '**/BuildConfig.*',
                       '**/Manifest*.*']
    )

    additionalSourceDirs = files(coverageSourceDirs)
    sourceDirectories = files(coverageSourceDirs)

    executionData = files('../app/build/jacoco/testDebug.exec')
    if (project.hasProperty("teamcity")) {
        println '##teamcity[jacocoReport dataPath=\'app/build/jacoco/testDebug.exec\' includes=\'com.mynamespace.myproject.*\' excludes=\'**/R.class **/R$*.class **/*$ViewInjector*.* **/BuildConfig.* **/Manifest*.*\']'
    }

    reports {
        xml.enabled = true
        html.enabled = true
    }

}

Teamcity 开始报告 CodeCoverage 如下:

【讨论】:

  • 这不考虑connectedAndroidTest结果。知道如何将它们包含在报告中吗?
【解决方案2】:

您可能需要考虑使用 Gradle jacoco plugin。这具有消除对 CI 基础架构的任何依赖的额外好处,允许您运行开发人员机器的覆盖率报告。

【讨论】:

  • 实际上,我已经在使用带有 Gradle 的 jacoco 插件,并且能够在开发人员的机器上运行覆盖率测试。但是,如果“测试代码”的数量减少,我想在 CI 服务器上运行以拒绝构建。
猜你喜欢
  • 2020-10-23
  • 1970-01-01
  • 2012-01-20
  • 2018-08-15
  • 2019-10-23
  • 2016-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多