Jacoco code root file is empty resulting in false lighting reports

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

We were having serious problems trying to get a code coverage report with Jacoco generated correctly.

We followed various guides on the internet including this one > Patrick McLaren, who he linked to in his own, to answer this question .

We've also covered various questions about stack overflow, but we're not happy about it yet.

Here's what we have so far (bullet-shaped so as not to read this question too long):

  • Adding testCoverageEnabled = true

    to close debug created a task called createDebugCoverageReport
  • Completing this task:

    • Produces a report for our tests on Android at: app / build / outputs / reports / androidTests / connected / index.html with accurate reporting of tests and crashes etc.
    • But inaccurate coverage report (0% coverage) at: app / build / results / reports / coverage / debug / index.html
    • It also creates what appears to be empty contains coverage data: app / build / output / 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 bits above) and run jacocoTestReport, we get:

  • Vanilla unit test test report: app / build / reports / tests / debug / index.html
  • Accurate code coverage report in app / assembly / reports / jacoco / jacocoTestReport / html / index.html

So we get the right thing for vanilla unit tests, but not for Android device testing.

Message / questions / 1207176 / android-jacoco-coverage-empty-with-gradle / 3918213 # 3918213

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

But perhaps it was reintroduced with regression? Are we missing something?

+3


source to share


2 answers


Although the answer is correct, the information below was the solution to our specific problem.

It turns out that for some bizarre reason, running tests on Samsung devices gives empty coverage files. Results of the same tests on a non-Samsung emulator or phone gave the desired results.



Including this information is here to let people know about it.

+8


source


Now (Oct 2015) you can use it as the android team has fixed the bug .

android {
    ...
    buildTypes {
        debug {
            testCoverageEnabled true
        }
    }
    ...
    dependencies{
        androidTestCompile 'com.android.support.test:runner:0.4.1'
        // Set this dependency to use JUnit 4 rules
        androidTestCompile 'com.android.support.test:rules:0.4.1'
        // Set this dependency to build and run Espresso tests
        androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
        // Espresso-contrib for DatePicker, RecyclerView, Drawer actions, Accessibility checks, CountingIdlingResource
        androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'
    }
}

      



Then just use ./gradlew createDebugCoverageReport

. Find reports in app/build/reports/coverage/debug/index.html

.

+1


source







All Articles