Coverage
emulator.wtf can capture code coverage from test runs for you with little extra configuration.
Preparation
To enable code coverage capturing, make sure that your (debug) app apk has JaCoCo code instrumentation enabled:
android {
buildTypes {
debug {
testCoverageEnabled true
}
}
}
The coverage output will be stored on the SD card folder in the emulator. To ensure this
works smoothly, add the WRITE_EXTERNAL_STORAGE
permission to your debug
or androidTest
manifest file (typically app/src/debug/AndroidManifest.xml or app/src/androidTest/AndroidManifest.xml
):
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Downloading captured code coverage
Captured code coverage will be stored in the outputs dir if running with coverage
is enabled. Our Gradle plugin will detect this automatically, but for CLI you’ll need to add the
--with-coverage
flag.
If you run tests with multiple shards and/or device models then there will be a separate coverage
file (.ec
file) for each shard/device model. Example, running a test on both NexusLowRes
and
Pixel2
while capturing coverage:
$ ew-cli --app path/to/app.apk --test path/to/test.apk \
--with-coverage --outputs-dir /tmp/foo \
--device model=NexusLowRes --device model=Pixel2
This command will capture two coverage files:
/tmp/foo/Pixel2_api27/coverage.ec
/tmp/foo/NexusLowRes_api27/coverage.ec
Known issues
Coverage, Test Orchestrator and clear package data
The combination of code coverage, test orchestrator and clear package data does not work on API version 29.
On emulators with API versions lower than 29 the WRITE_EXTERNAL_STORAGE
permission will be lost between
each test run. You can fix this by requesting the permission every time before a test runs using a
test rule in your test classes:
@get:Rule
val writeRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE)
@Rule
public GrantPermissionRule writeRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE);