blob: e16299451f581c00cdd5b3fcca88aa5b8b74d607 [file] [log] [blame] [view]
hzl1ccac192016-06-02 18:22:121# Android code coverage instructions
2
mvanouwerkerk0ac0a372016-10-18 09:00:313These are instructions for collecting code coverage data for android
4instrumentation and junit tests.
hzl1ccac192016-06-02 18:22:125
6[TOC]
7
Yun Liueaf7a4542019-05-15 17:27:398## How Jacoco coverage works
hzl1ccac192016-06-02 18:22:129
Yun Liueaf7a4542019-05-15 17:27:3910In order to use Jacoco code coverage, we need to create build time pre-instrumented
Yun Liu435bb172019-05-17 21:44:3411class files and runtime **.exec** files. Then we need to process them using the
Yun Liueaf7a4542019-05-15 17:27:3912**build/android/generate_jacoco_report.py** script.
hzl1ccac192016-06-02 18:22:1213
Yun Liu435bb172019-05-17 21:44:3414## How to collect coverage data
hzl1ccac192016-06-02 18:22:1215
mvanouwerkerk0ac0a372016-10-18 09:00:31161. Use the following GN build arguments:
mvanouwerkerk0ac0a372016-10-18 09:00:3117
Yun Liu435bb172019-05-17 21:44:3418 ```gn
19 target_os = "android"
20 jacoco_coverage = true
21 ```
David 'Digit' Turner40560ef72018-03-07 09:44:2822
Yun Liu435bb172019-05-17 21:44:3423 Now when building, pre-instrumented files will be created in the build directory.
David 'Digit' Turner40560ef72018-03-07 09:44:2824
hzl1ccac192016-06-02 18:22:12252. Run tests, with option `--coverage-dir <directory>`, to specify where to save
Yun Liueaf7a4542019-05-15 17:27:3926 the .exec file. For example, you can run chrome junit tests:
mvanouwerkerk0ac0a372016-10-18 09:00:3127 `out/Debug/bin/run_chrome_junit_tests --coverage-dir /tmp/coverage`.
David 'Digit' Turner40560ef72018-03-07 09:44:2828
Yun Liueaf7a4542019-05-15 17:27:39293. The coverage results of junit and instrumentation tests will be merged
mvanouwerkerk0ac0a372016-10-18 09:00:3130 automatically if they are in the same directory.
David 'Digit' Turner40560ef72018-03-07 09:44:2831
Yun Liu435bb172019-05-17 21:44:3432## How to generate coverage report
David 'Digit' Turner40560ef72018-03-07 09:44:2833
Yun Liu435bb172019-05-17 21:44:34341. Now we have generated .exec files already. We can create a Jacoco html/xml/csv
35 report using `generate_jacoco_report.py`, for example:
36
37 ```shell
38 build/android/generate_jacoco_report.py \
39 --format html \
40 --output-dir tmp/coverage_report/ \
41 --coverage-dir /tmp/coverage/ \
42 --sources-json-dir out/Debug/ \
43 ```
Yun Liueaf7a4542019-05-15 17:27:3944 Then an index.html containing coverage info will be created in output directory:
David 'Digit' Turner40560ef72018-03-07 09:44:2845
Yun Liu435bb172019-05-17 21:44:3446 ```
47 [INFO] Loading execution data file /tmp/coverage/testTitle.exec.
48 [INFO] Loading execution data file /tmp/coverage/testSelected.exec.
49 [INFO] Loading execution data file /tmp/coverage/testClickToSelect.exec.
50 [INFO] Loading execution data file /tmp/coverage/testClickToClose.exec.
51 [INFO] Loading execution data file /tmp/coverage/testThumbnail.exec.
52 [INFO] Analyzing 58 classes.
53 ```
Yun Liueaf7a4542019-05-15 17:27:3954
55 For xml and csv reports, we need to specify `--output-file` instead of `--output-dir` since
56 only one file will be generated as xml or csv report.
Yun Liu435bb172019-05-17 21:44:3457 ```shell
58 build/android/generate_jacoco_report.py \
59 --format xml \
60 --output-file tmp/coverage_report/report.xml \
61 --coverage-dir /tmp/coverage/ \
62 --sources-json-dir out/Debug/ \
63 ```
Yun Liueaf7a4542019-05-15 17:27:3964
Yun Liu435bb172019-05-17 21:44:3465 or
66
67 ```shell
68 build/android/generate_jacoco_report.py \
69 --format csv \
70 --output-file tmp/coverage_report/report.csv \
71 --coverage-dir /tmp/coverage/ \
72 --sources-json-dir out/Debug/ \
73 ```
74
752. We can also generate JSON format report for
76 [coverage tool](https://chromium.googlesource.com/chromium/src/+/HEAD/docs/code_coverage.md)
77 created by Chrome Ops - CATS team.
78 In this case, we need to change `--format` to json, for example:
79
80 ```shell
81 build/android/generate_jacoco_report.py \
82 --format json \
83 --output-file tmp/coverage_report/coverage.json \
84 --coverage-dir /tmp/coverage/ \
85 --sources-json-dir out/Debug/ \
86 ```
87