Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jacoco code covergage file empty leading to false coverage reports

A Github repo with the code used for this question can be found here: https://github.com/thenewmr/UnitTestCoverageExample

We've been having serious issues trying to get a code coverage report via Jacoco generated correctly.

We've followed various guides on the internet including this one from Patrick McLaren that he linked to in his answer to this question.

We've also looked at various questions on stack overflow but no joy so far.

Here's what we have so far (in bullet point form so as not to make this question too long to read):

  • Adding testCoverageEnabled = true to the debug closure created a task called "createDebugCoverageReport"
  • Running this task:

    • Produces a report for our Android tests at: app/build/outputs/reports/androidTests/connected/index.html with an accurate report of test passes and failures etc
    • But an inaccurate coverage report (0% coverage) at: app/build/outputs/reports/coverage/debug/index.html
    • It also produces what appears to be empty coverage data at: app/build/outputs/code-coverage/connected/coverage.ec
  • Now, if we add the following:

    apply plugin: 'jacoco'
    
    //specify which directories should be examined by jacoco
    def coverageSourceDirs = [
            'src/main/java'
    ]
    
    task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug") {
        group = "Reporting"
    
        description = "Generate Jacoco coverage reports"
    
        classDirectories = fileTree(
                dir: 'build/intermediates/classes/debug',
                excludes: ['**/R.class',
                           '**/R$*.class',
                           '**/*$ViewInjector*.*',
                           '**/BuildConfig.*',
                           '**/Manifest*.*']
        )
    
        additionalSourceDirs = files(coverageSourceDirs)
        sourceDirectories = files(coverageSourceDirs)
        executionData = files('build/jacoco/testDebug.exec')
    
        reports {
            xml.enabled = true
            html.enabled = true
        }
    }
    

to the build.gradle file for the app (with or without the bit above) and run jacocoTestReport, we get:

  • A test report for the vanilla Unit tests at: app/build/reports/tests/debug/index.html
  • An accurate code coverage report at app/build/reports/jacoco/jacocoTestReport/html/index.html

So, we get the correct stuff for the vanilla unit tests but not for the Android unit tests.

The blog post mentioned above talks about how to combine the two reports. But it seems it would be pointless if we can’t get the Android test coverage report produced in the first place.

The problem appears to be due to the empty coverage.ec file as mentioned earlier.

This answer says this used to be a bug: https://stackoverflow.com/a/28080713/487812

and this bug report says this problem was fixed: https://code.google.com/p/android/issues/detail?id=78556

But perhaps it's been reintroduced since as a regression? Are we missing something obvious?

like image 434
the_new_mr Avatar asked Dec 24 '22 16:12

the_new_mr


1 Answers

Though nius' answer is correct, the info below was the solution to our particular problem.

Turns out that, for some bizarre reason, running the tests on Samsung devices yields empty coverage files. Running the same tests on an emulator or non-Samsung phone produced the desired results.

Including this info here so that people are aware of this.

like image 104
the_new_mr Avatar answered Dec 28 '22 09:12

the_new_mr