| # Android code coverage instructions |
| |
| These are instructionsfor collecting code coverage datafor android |
| instrumentationandJUnit tests.ForClang(C++) code coverage refer to[clang coverage]. |
| |
| [TOC] |
| |
| ## How JaCoCo coverage works |
| |
| In order touseJaCoCo code coverage, we need to create build time pre-instrumented |
| class filesand runtime**.exec** files.Then we need to process themusing the |
| [build/android/generate_jacoco_report.py](https://source.chromium.org/chromium/chromium/src/+/main:build/android/generate_jacoco_report.py) script. |
| |
| ## How to collect coverage data |
| |
| 1.Use the following GN build arguments: |
| |
| ```gn |
| target_os = "android" |
| use_jacoco_coverage = true |
| ``` |
| |
| Nowwhen building, pre-instrumented files will be createdin the build directory. |
| |
| 2.Run tests,with option`--coverage-dir <directory>`, to specifywhere to save |
| the.exec file.For example, you can run chromeJUnit tests: |
| `out/Debug/bin/run_chrome_junit_tests --coverage-dir /tmp/coverage`. |
| |
| 3.The coverage results ofJUnitand instrumentation tests will be merged |
| automaticallyif they arein the same directory. |
| |
| ## How to generate coverage report |
| |
| 1.Now we have generated.exec files already.We can create aJaCoCo HTML/XML/CSV |
| reportusing`generate_jacoco_report.py`,for example: |
| |
| ```shell |
| build/android/generate_jacoco_report.py \ |
| --format html \ |
| --output-dir /tmp/coverage_report/ \ |
| --coverage-dir /tmp/coverage/ \ |
| --sources-json-dir out/Debug/ \ |
| ``` |
| Then an index.html containing coverage info will be createdin output directory: |
| |
| ``` |
| [INFO] Loading execution data file /tmp/coverage/testTitle.exec. |
| [INFO] Loading execution data file /tmp/coverage/testSelected.exec. |
| [INFO] Loading execution data file /tmp/coverage/testClickToSelect.exec. |
| [INFO] Loading execution data file /tmp/coverage/testClickToClose.exec. |
| [INFO] Loading execution data file /tmp/coverage/testThumbnail.exec. |
| [INFO] Analyzing 58 classes. |
| ``` |
| |
| 2.For XMLand CSV reports, we need to specify`--output-file` instead of`--output-dir` since |
| only one file will be generatedas XMLor CSV report. |
| ```shell |
| build/android/generate_jacoco_report.py \ |
| --format xml \ |
| --output-file /tmp/coverage_report/report.xml \ |
| --coverage-dir /tmp/coverage/ \ |
| --sources-json-dir out/Debug/ \ |
| ``` |
| |
| or |
| |
| ```shell |
| build/android/generate_jacoco_report.py \ |
| --format csv \ |
| --output-file /tmp/coverage_report/report.csv \ |
| --coverage-dir /tmp/coverage/ \ |
| --sources-json-dir out/Debug/ \ |
| ``` |
| 3.If generating coverageand there are duplicateclass files,as can happen |
| when generating coveragefor downstream targets,use the |
| `--include-substr-filter` option to choose jarsin the desired directory.Eg. |
| for generating coverage reportforClankinternal repo |
| ```shell |
| build/android/generate_jacoco_report.py --format html \ |
| --output-dir /tmp/coverage_report/ --coverage-dir /tmp/coverage/ \ |
| --sources-json-dir out/java_coverage/ \ |
| --include-substr-filter obj/clank |
| ``` |
| |
| [clang coverage]: https://chromium.googlesource.com/chromium/src/+/HEAD/docs/testing/code_coverage.md |