简体   繁体   中英

Gradle | Jacoco Task | test report

In some blog, I found below task to generate the jacoco report:

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')
}

I am new to Gradle and I want to understand each step of this task in detail. Below are my queries:

  1. 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.

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

  3. What is the difference between the pattern

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

    Both are excluding the R files from report then what is the difference?

  4. Why different pattern for R class and Android classes?

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

  5. 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.

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

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.

This ensures that testDebugUnitTest runs before jacocoTestReport. You usually set up such task dependencies because one task depends on the output of another. In this case, you want the tests to run — via testDebugUnitTest — before you try to generate a report for them.

BTW, I believe Jacoco works by instrumenting the class files generated by the compiler. This instrumentation generates data that Jacoco can then analyse to determine whether methods are called or not. But you need to execute the code in order to get that data, hence why you run the tests first.

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

This is a task. Tasks can be defined in your build script, a parent build script or added via plugins. The example code you show doesn't tell us anything about where this task is coming from.

Having said that, it seems that the Android plugin sets this task up.

What is the difference between the pattern

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

Compiled inner and anonymous class files are named as '$.class'. This is just ensuring that Jacoco picks up those inner and anonymous classes.

Why different pattern for R class and Android classes?

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

No idea. I don't know what this R class is. The Android pattern is simply narrower, since it's working on the basis that Android classes are somewhere in an android folder.

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

I don't know for sure, but it seems that sourceDirectories is for source sets . Note that the task has a sourceSets() method. This populates the sourceDirectories file collection.

additionalSourceDirectories appears to be for other source directories that aren't defined as part of a source set.

Honestly, this task seems to be pretty badly documented.

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

I'm guessing that testDebugUnitTest.exec is a file that is generated when you run the instrumented classes via the debug unit tests. executionData is a way to tell the JacocoReport task where to find that file. But as I said, I'm taking an educated guess.

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.

  • That means that your task call depended task before itself execute.

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

  • That is predefined job which starts all Tests.

What is the difference between the pattern

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

  • ' * * /R.class' - files with R.class name in any path
  • ' * * /R$*.class' - files with R prefix in name and .class extension in any path

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/* * /* . * ' also excluded some different files which complines that pattern

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.

  • No difference. additionalSourceDirs should be used for third party components if it make sense.

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

  • executionData - that is set of tests to run

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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