Gradle | Jacoco 任务 |测试报告

Gradle | Jacoco Task | test report

在一些博客中,我发现了以下生成 jacoco 报告的任务:

task jacocoTestReport(type: JacocoReport, dependsOn: "testDebugUnitTest") {
    group = "Verification"
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled = true
        html.enabled = true
    }

    classDirectories = fileTree(
        dir: "${project.buildDir}/intermediates/classes/debug",
        excludes: ['**/R.class', 
                   '**/R$*.class', 
                   '**/BuildConfig.*', 
                   '**/Manifest*.*', 
                   'android/**/*.*'
        ])

    additionalSourceDirs = files(coverageSourceDirs)
    sourceDirectories = files(coverageSourceDirs)
    executionData = files('build/jacoco/testDebugUnitTest.exec')
}

我是 Gradle 的新手,我想详细了解此任务的每个步骤。以下是我的查询:

  1. 创建新任务时 dependsOn: "testDebugUnitTest" 的功能是什么?即使我不写这个声明,我仍然能够生成报告。

  2. 什么是 testDebugUnitTest?它是如何以及在何处生成的?

  3. 模式有什么区别

    ' * * /R.class' 和 ' * * /R$*.class'

    两者都从报告中排除了 R 文件,有什么区别?

  4. 为什么 R class 和 Android class 的模式不同?

    '* * /R.class' 对比 'android/* * /* . * '

  5. additionalSourceDirs 和源目录有什么区别?根据文档,

  6. 的描述相同

Source sets that coverage should be reported for.

  1. 什么是 executionData & testDebugUnitTest.exec? testDebugUnitTest.exec 是自动生成的吗?为什么我们需要提及它?

What is the functionality of dependsOn: "testDebugUnitTest" while creating a new task? Even if I don't put this statement, still I am able to generate the report.

  • 这意味着您的任务在执行之前调用依赖的任务。

What is testDebugUnitTest? How & where it's generated?

  • 这是启动所有测试的预定义作业。

What is the difference between the pattern

' * * /R.class' and ' * * /R$*.class'

  • ' * * /R.class' - 任何路径中具有 R.class 名称的文件
  • ' * * /R$*.class' - 名称中带有 R 前缀且任何路径中带有 .class 扩展名的文件

Both are excluding the R files from report then what is the difference? Why different pattern for R class and Android classes?

'* * /R.class' vs 'android/* * /* . * '

  • 'android/* * /* . * ' 还排除了一些符合该模式的不同文件

What is the difference between additionalSourceDirs & source directories? As per the documentation, description is same for both Source sets that coverage should be reported for.

  • 没有区别。如果有意义,应该将 additionalSourceDirs 用于第三方组件。

What is executionData & testDebugUnitTest.exec? Is testDebugUnitTest.exec autogeenerated and why we need to mention this ?

  • executionData - 这是 运行
  • 的一组测试

What is the functionality of dependsOn: "testDebugUnitTest" while creating a new task? Even if I don't put this statement, still I am able to generate the report.

这确保 testDebugUnitTest 运行s 在 jacocoTestReport 之前。您通常会设置此类任务依赖性,因为一个任务依赖于另一个任务的输出。在这种情况下,您希望测试 运行 — 通过 testDebugUnitTest — 在您尝试为它们生成报告之前。

顺便说一句,我相信 Jacoco 通过检测编译器生成的 class 文件来工作。该仪器生成数据,然后 Jacoco 可以分析这些数据以确定是否调用了方法。但是您需要执行代码才能获取该数据,因此为什么要先 运行 测试。

What is testDebugUnitTest? How & where it's generated?

这是一个任务。任务可以在您的构建脚本、父构建脚本中定义或通过插件添加。您显示的示例代码没有告诉我们有关此任务来自何处的任何信息。

话虽如此,看来 Android 插件设置了这个任务。

What is the difference between the pattern

' * * /R.class' and ' * * /R$*.class'

已编译的内部和匿名 class 文件被命名为“$.class”。这只是确保 Jacoco 获取那些内部和匿名的 classes.

Why different pattern for R class and Android classes?

'* * /R.class' vs 'android/* * /* . * '

不知道。我不知道这个 R class 是什么。 Android 模式只是更窄,因为它的工作基础是 Android classes 位于 android 文件夹中。

What is the difference between additionalSourceDirs & source directories? As per the documentation, description is same for both

我不确定,但 sourceDirectories 似乎适用于 源集 。请注意,该任务有一个 sourceSets() 方法。这会填充 sourceDirectories 文件集合。

additionalSourceDirectories 似乎适用于未定义为源集一部分的其他源目录。

老实说,这个任务的记录似乎很糟糕。

What is executionData & testDebugUnitTest.exec? Is testDebugUnitTest.exec autogeenerated and why we need to mention this ?

我猜 testDebugUnitTest.exec 是当您 运行 通过调试单元测试检测 classes 时生成的文件。 executionData 是一种告诉 JacocoReport 任务在哪里可以找到该文件的方法。但正如我所说,我正在做一个有根据的猜测。