I'm using ./gradlew createDebugCoverageReport
to generate a code coverage report of all my Android instrumentation (Robotium) tests. But I need to run all tests in order to get the coverage report now. How can I specify one single test (or single test class) to execute and get the coverage report? I beed it during development of the tests, it's too slow having to run all tests at once.
To get code coverage reports in a Maven project, we first need to set up the JaCoCo Maven plugin for that project. By integrating the JaCoCo plugin, the results of the code coverage analysis can be reviewed as an HTML report. The current version of the JaCoCo-Maven plugin can be downloaded from the MVN Repository.
View test coverage To run tests with coverage, follow the same steps as described in Run tests, only instead of clicking Run , click Run test-name with coverage . In the Project window, this option might be hidden behind More Run/Debug.
Run a test with code coverageOpen the desired file in the Project tool window and choose Run <name> with Coverage from the context menu. You can also select a directory with test files and choose the corresponding command from the context menu to run several tests with coverage.
I know this is an old post, but here's how I do it.
(Flavor may vary. In this case, it's debug
.)
// install instrumentation test app if you have not
./gradlew installDebugAndroidTest
In my case, I chose ClassName#methodName
.
// execute one test
adb shell am instrument -w -r --no_window_animation -e coverageFile /data/data/com.org.android.test/coverage.ec -e class 'com.org.android.ClassName#methodName', -e coverage true com.org.android.test/android.support.test.runner.AndroidJUnitRunner
Note that I'm passing 2 parameters:
A. -e coverageFile /data/data/com.org.android.test/coverage.ec
and,
B. -e coverage true
The two options would generate a coverage report within the device.
If you are not familiar with running tests by adb shell am
command, please refer to this official documentation.
// get coverage.ec data
adb shell run-as com.org.android.test cat /data/data/com.org.android.test/coverage.ec | cat > [YOUR_PROJECT_DIRECTORY]/build/outputs/code_coverage/debugAndroidTest/connected/coverage.exec
There are two things to note here.
A. You should change [YOUR_PROJECT_DIRECTORY]
to your project directory. Or, you can change the entire [YOUR_PROJECT_DIRECTORY]/build/outputs/code_coverage/debugAndroidTest/connected/coverage.exec
to any directory and filename you want. (maybe desktop?)
B. but the final content should have extension .exec
, because jacoco will only accept those.
In Android Studio, please navigate to run
> Show Code Coverage Data
. Then a select window will appear. Select previously generated coverage.exec
. Then the Android Studio will process the data and show you the coverage data. You can directly view the code coverage data, or further generate coverage report.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With