Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate code coverage reports for Android unit tests

I'd like to start generating unit test code coverage reports for my Android application, where the unit tests are not in the androidTest folder, but in a test folder that I've created. To start, I've added the following to my build.gradle file:

buildTypes {
    ...
    debug {
        debuggable true
        testCoverageEnabled true
    }
    ...
}

Running ./gradlew createDebugCoverageReport generates reports for my tests in the androidTest folder, but nothing in the test folder. How can I create those same coverage reports for the tests in the test folder?

like image 951
M B Avatar asked Dec 08 '16 21:12

M B


People also ask

How do I get code coverage on Android?

Android Studio has a built-in feature that allows you to run tests with code coverage. Simply navigate to the src/test/java folder and right click. Then select Run 'Tests in 'java'' with Coverage (awkward use of single quotes theirs not mine).

How do you calculate unit testing code coverage?

To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.

How do I get a JUnit coverage report?

Run the JUnit. View the results. The results of the run are available in the Code Coverage Results view. If you do not see this view, select Window > Show View > Other > Code Coverage > Code Coverage Results.


1 Answers

I've founded answer here https://stackoverflow.com/a/23965581/1775228 Basically, you add to your gradle file:

debug {
    testCoverageEnabled true
}

and

android {
    jacoco {
        version = '0.7.9'
    }
}

After that run

./gradlew createDebugCoverageReport

in terminal and report will be generated in

/build/reports/coverage/debug

in your app folder/module. It worked for me.


For latest version and updates regarding changes read The JaCoCo Plugin.

like image 129
NixSam Avatar answered Nov 01 '22 11:11

NixSam