Mobile app coverage
CoverOps measures which code of an Android or iOS app your tests executed — Espresso, XCTest, XCUITest, Appium or a person testing by hand — per run and, where the tests say where one test ends and the next begins, per test. The app then appears as a service of the run, beside the backend services your agents measure, and takes part in coverage, test selection and the changed-code gate like any other service.
It is new. What was verified lists exactly what we have run and what we have not.
How it works
Three parts, the same on both platforms:
- The build. A test build of the app is compiled with the platform's own coverage: JaCoCo's offline instrumentation on Android, LLVM source-based coverage on iOS. The app now counts which code runs.
- The collector, a small library in that build. It writes the counters to files in the app's private storage, cut at test boundaries: the time between tests is one segment, each test is one. Every segment is a data file (a JaCoCo
.ec, an LLVM.profraw) and a small JSON file naming the test and, when the collector knows it, its verdict. - The command on the build host:
coverageops android-pullorcoverageops ios-pull. It copies the files off the emulator, simulator or device, converts them with JaCoCo's or LLVM's own tools against the classes or the binary of the build that ran, maps every file to its repository path, and uploads like an agent: the app is the service (its package or bundle id, or--service), the device is the instance, the runtime isandroidorios.
The conversion happens on the build host, not on the phone and not on the CoverOps server: the coverage data is probe flags (JaCoCo) or counters (LLVM) keyed by class checksum or function hash, and only the class files or the app binary of that exact build can turn them into source lines. They exist where the app was built.
Android
Set up the build
// settings.gradle.kts — the collector is served by your CoverOps, not Maven Central
dependencyResolutionManagement {
repositories { google(); mavenCentral(); maven { url = uri("<API_URL>/v1/maven") } }
}
// app/build.gradle.kts
android {
buildTypes { debug { enableAndroidTestCoverage = true } } // AGP 7.3+; older: testCoverageEnabled = true
defaultConfig {
// Espresso / AndroidX Test: cut per test and record verdicts
testInstrumentationRunnerArguments["listener"] = "io.coverageops.mobile.CoverOpsRunListener"
}
}
dependencies {
debugImplementation("io.coverageops:coverageops-android:0.1.0")
}
The AAR (also a download on the Agents page, Android tile) carries:
CoverOpsCollector— reads JaCoCo's runtime through its public API,org.jacoco.agent.rt.RT.getAgent().getExecutionData(reset), by reflection. A build without JaCoCo makes it a no-op. JaCoCo's offline runtime starts with the first instrumented class that runs, so the collector looks it up when it first needs it.CoverOpsInitProvider— starts the collector when the app process starts, beforeApplication.onCreate, in a debuggable build only. It flushes every 10 seconds (<meta-data android:name="io.coverageops.flush_seconds" android:value="…"/>, 0 for boundaries only).CoverOpsRunListener— the JUnit 4 listener for instrumented tests.CoverOpsReceiver— test boundaries sent from the host (Appium, adb).jacoco-agent.propertieswithoutput=none, so JaCoCo's runtime does not try to write its own file on exit.
Files go to the app's files/coverops/.
After the tests
npm install -g <API_URL>/v1/agents/artifact/cli
export COVERAGEOPS_ENDPOINT=<API_URL> COVERAGEOPS_TOKEN=cov_…
coverageops android-pull --package com.acme.shop --module app
| Flag | Meaning |
|---|---|
--package | The application id. The files are read with adb exec-out run-as <package>, which needs a debuggable build. |
--serial | The device (ANDROID_SERIAL); also the instance the coverage is filed under. |
--from DIR | Read files already pulled instead: AGP's connected_android_test_additional_output, an Appium pullFile, a device farm's artifacts. |
--module, --variant | The Gradle module (default .) and variant (default debug). The class files are found under build/intermediates/javac/<variant>/…, build/tmp/kotlin-classes/<variant> and build/intermediates/built_in_kotlinc/<variant>/…; the sources under src/main/{java,kotlin} and src/<variant>/…. |
--classes, --sources | Explicit directories, comma-separated, when the layout differs. |
--jacoco-cli JAR | JaCoCo's CLI (org.jacoco.cli-<version>-nodeps.jar; or COVERAGEOPS_JACOCO_CLI). Without it, the newest copy in Gradle's cache is used. Needs java. |
--jacoco-xml FILE | Skip the conversion and read a JaCoCo XML report your build made (createDebugCoverageReport, a JacocoReport task): the run only, no tests. |
--clear | Delete the files on the device after the upload was accepted. |
--keep-unresolved | Keep classes whose source file is not in the source sets (by default generated code — R, BuildConfig — is left out). |
Paths. JaCoCo names a file by package and file name (com/acme/shop/Cart.kt). Kotlin does not require the folder to follow the package, so the file is looked up in the source sets — by path first, else by name and package declaration — and reported relative to the repository root (app/src/main/java/com/acme/shop/Cart.kt). That is the path a commit's diff names, so the gate and test selection match it exactly.
Per test, by framework
| Framework | How tests are cut | Verdicts |
|---|---|---|
| Espresso, UI Automator, any JUnit 4 test under AndroidJUnitRunner | CoverOpsRunListener (-e listener …) dumps and resets at every test's start and end, in the app's process. Named <test class>::<method>; @CoverOpsTestId("AUT-45") adds the test-management id. | Passed, failed (with the message), skipped (assumption failures and @Ignore), sent with tool: espresso. |
| Appium (UiAutomator2), Maestro, a shell script | A broadcast before and after each test: mobile: broadcast with action io.coverageops.mobile.TEST_START, component <package>/io.coverageops.mobile.android.CoverOpsReceiver and the extra test (and optionally test_id); then TEST_END. FLUSH writes what ran so far. | None from the app; the suite's runner integration (JUnit 5, TestNG, pytest, WebdriverIO, Robot Framework) reports them under the same name. |
| Manual testing | No boundaries: the whole session is one segment, flushed every 10 s. | — |
With the Android Test Orchestrator and clearPackageData, the app's files are wiped after every test. Point the instrumentation argument coverOpsDir at a folder that survives, or leave clearPackageData off.
iOS
Set up the build
Coverage on for the build the tests run against: Xcode's Gather coverage for a test plan, or, for an app Appium launches, the flags in a coverage configuration:
OTHER_SWIFT_FLAGS = $(inherited) -profile-generate -profile-coverage-mapping
OTHER_CFLAGS = $(inherited) -fprofile-instr-generate -fcoverage-mapping
OTHER_LDFLAGS = $(inherited) -fprofile-instr-generate
Add the Swift package (download it from the Agents page, iOS tile, and add it as a local package): CoverOpsCollector to the app target, CoverOpsXCTest to test targets.
// App start, in the coverage build
#if DEBUG
import CoverOpsCollector
CoverOps.start() // files go to Library/Caches/coverops
#endif
The package's CoverOpsProfile target declares LLVM's profile runtime API (__llvm_profile_set_filename, __llvm_profile_write_file, __llvm_profile_reset_counters) with weak, do-nothing definitions: when coverage is on, the runtime's own definitions replace them; when it is off, the app still links and CoverOps.start() returns false. COVEROPS_DISABLE=1 in the environment turns it off in a coverage build (Xcode's own coverage report then works as before).
The collector writes the open segment every 5 seconds, when the app goes to the background or terminates, and on the Darwin notification io.coverageops.flush (it answers io.coverageops.flushed).
After the tests
export COVERAGEOPS_ENDPOINT=<API_URL> COVERAGEOPS_TOKEN=cov_…
coverageops ios-pull --simulator booted --bundle-id com.acme.Shop \
--binary build/Build/Products/Debug-iphonesimulator/Shop.app
| Flag | Meaning |
|---|---|
--simulator UDID|booted | Copy from a simulator's app container (xcrun simctl get_app_container). |
--device ID | Copy from a device with xcrun devicectl device copy from (Xcode 15+; a development-signed app). |
--from DIR | Read files already pulled (an Appium pullFile, a device farm's artifacts). |
--binary | The app that ran (Shop.app or Shop.app/Shop), plus any instrumented frameworks, comma-separated. |
--path-map FROM=TO | Rewrite the build machine's checkout path to this one, when the app was built elsewhere. |
--repo-root | The repository root paths are reported relative to (default: git's top level). Files outside it — SDKs, DerivedData, SourcePackages, Pods, Carthage — are left out. |
--ignore REGEX | More files to leave out (--no-default-ignore drops the default list). |
--llvm-profdata, --llvm-cov | The tools; on a Mac xcrun finds the ones of the selected Xcode. A profile is read only by the LLVM release that wrote it: use the Xcode that built the app. |
The command merges the profiles with llvm-profdata merge -sparse, exports LCOV with llvm-cov export -format=lcov against the binary (once for the run, once per test), and, for metadata-only projects, reads each function's line range from llvm-cov export -format=text.
Per test, by framework
| Framework | How tests are cut | Verdicts |
|---|---|---|
| XCTest unit and integration tests hosted in the app | CoverOpsTestObserver.register() (an XCTestObservation) cuts at each test's start and end in the app's process. Named <test class>::<method>. | Passed, failed (the first issue's message), skipped; tool: xctest. |
| XCUITest | The app runs in its own process, so each test launches it with its name: app.launchForCoverOps(self) sets COVEROPS_TEST; CoverOpsUITest.flush() in tearDown asks the app to write before the test ends it. | None from the app; post them with the Results API. |
| Appium (XCUITest driver) | mobile: launchApp with environment: {COVEROPS_TEST: "Suite::name"} per test; send the app to the background (mobile: backgroundApp) before ending it, so it writes. | From the suite's runner integration. |
| Manual testing | The session is one segment. | — |
On iOS a test is one launch: a suite that keeps one app process across several UI tests is attributed per launch.
Shared behaviour
- Run key.
--run-key, else an open monitoring window (as for every agent), elseCOVERAGEOPS_RUN_KEY, else the CI build id, elselocal-YYYYMMDD. Give the Appium runner the same key so verdicts, app coverage and backend coverage share one run. - Protocol. The command polls, joins the run, uploads the run's LCOV and the per-test lines (with verdicts when the collector knows them), gzipped, with the runtime, instance and environment headers. A
401,402or413is printed with the server's code and message and exits 2 (--lcov-out FILEkeeps the converted report for another try); a5xx(503 INGEST_BUSYincluded),408,429, a timeout or a lost connection is sent again after the server'sRetry-After(at most 60 s) or a growing random wait, at most 5 tries, within 30 s for the poll and joining the run and 120 s for the uploads (COVERAGEOPS_RETRY_ATTEMPTS,COVERAGEOPS_RETRY_SECONDS; see Retries).--printshows what would be sent and sends nothing. - Source. After the upload, the command asks what the project keeps: in
storemode it sends the text of the covered files the server asks for (relative paths inside the repository only); in metadata-only mode it sends outlines — line counts, line kinds, digests and method ranges (from JaCoCo's methods on Android, an estimate; from LLVM's function regions on iOS), never text.--no-sourceskips both. - Instances. Each device is an instance of the app, and counts toward the package's instance limit like a pod.
- The backend. The collectors do not add the
X-CoverageOps-Testheaders to the app's own HTTP requests. Backend coverage of a mobile run is per run or window unless the app's test build adds them (see Test context).
What was verified
| Verified | How | |
|---|---|---|
| Android collector and listener | Yes, on a JVM | JaCoCo 0.8.13's real offline instrumentation and runtime (jacococli instrument + org.jacoco.agent runtime jar), JUnit 4 tests run with CoverOpsRunListener: segments, sidecars, verdicts, the lazy runtime lookup, no-op without the runtime (agents/android/tests/run.sh). |
| The AAR | Built and its layout checked | javac against JUnit and the Android API stubs, packed as AndroidManifest.xml + classes.jar + R.txt. Not yet consumed by an Android Gradle build or installed on an emulator. |
android-pull conversion | Yes | With the real jacococli on the exec files the listener wrote, and on a checked-in copy of such a run (packages/cli/test/fixtures/mobile/android). |
| iOS collector | Yes, on Linux | Swift 5.10's toolchain: the package links with and without coverage (weak stubs replaced by the runtime), XCTest tests under CoverOpsTestObserver give one profile per test with verdicts, a launch named by COVEROPS_TEST is written at exit (agents/ios/tests/run.sh). |
ios-pull conversion | Yes, on Linux | Real llvm-profdata and llvm-cov on those profiles; llvm-cov also read a Mach-O (iOS simulator) object's coverage mapping on Linux. |
| Server | Yes | Runtimes android / ios on the fleet and the run's service cards, the device as instance, per-test lines, repository paths meeting a commit's paths (apps/api/test/mobile-runtime.test.ts). |
| Emulators, simulators, devices, device farms | No | Not yet run: adb run-as and simctl / devicectl pulls, UIKit background flushes, Darwin notifications between a UI test and the app, Appium's mobile: broadcast and mobile: launchApp, AGP's handling of the AAR, Xcode's toolchains. |
Limits
- A test build only: debuggable on Android, development-signed on iOS. Release builds are not measured.
- Native code only: Kotlin, Java, Swift, Objective-C (and C/C++ compiled into the app with LLVM coverage on iOS). React Native's JavaScript and Flutter's Dart are not measured.
- iOS: a dynamic framework has its own copy of the profile runtime; its coverage is written at exit to the default file and is not cut per test. Statically linked code (most Swift packages) is.
- JaCoCo records whether code ran, not how often: Android line counts are 0 or 1.
- Between writing a segment and zeroing the counters, a line that runs in that instant can be lost from both segments (LLVM); JaCoCo's
getExecutionData(true)does both at once. - Coverage the app recorded after the last flush is lost if the app is killed: Android flushes every 10 s and at each boundary; iOS every 5 s, on background and at each boundary.
How it was designed
From maintained open-source work, used as tools or read for design only — no code copied:
- JaCoCo (EPL-2.0) — used as a tool: its offline instrumentation and runtime in the app (through the Android Gradle plugin), its CLI for the conversion. The collector calls only its public runtime API, by reflection.
- Android's on-device coverage and AndroidX Test's coverage listener — the pattern of dumping the runtime's data from the test process and pulling it with
run-as; CoverOps adds a cut per test. - Appium's Android coverage guide — a receiver that dumps on a broadcast; CoverOps uses
mobile: broadcast, which needs neither a custom instrumentation nor a rooted device. - LLVM source-based coverage —
__llvm_profile_*runtime API,llvm-profdata merge,llvm-cov export. - Slather and xcov (MIT) — profile + binary through
llvm-cov, and leaving out third-party paths; read for design.