blob: 17c83c68693da16b570f236fb637f7fea47f6bc9 [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
Yun Liu02ae34b2019-07-18 22:01:444instrumentation and JUnit tests.
hzl1ccac192016-06-02 18:22:125
6[TOC]
7
Yun Liu02ae34b2019-07-18 22:01:448## How JaCoCo coverage works
hzl1ccac192016-06-02 18:22:129
Yun Liu02ae34b2019-07-18 22:01:4410In 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"
Yun Liuc0f2f732019-09-18 17:06:3120 use_jacoco_coverage = true
Yun Liu435bb172019-05-17 21:44:3421 ```
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 Liu02ae34b2019-07-18 22:01:4426 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 Liu02ae34b2019-07-18 22:01:44293. 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 Liu02ae34b2019-07-18 22:01:44341. Now we have generated .exec files already. We can create a JaCoCo HTML/XML/CSV
Yun Liu435bb172019-05-17 21:44:3435 report using `generate_jacoco_report.py`, for example:
36
37 ```shell
38 build/android/generate_jacoco_report.py \
39 --format html \
Finnur Thorarinssonab966862020-03-27 15:31:3840 --output-dir /tmp/coverage_report/ \
Yun Liu435bb172019-05-17 21:44:3441 --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
Yun Liu02ae34b2019-07-18 22:01:44552. 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 \
Finnur Thorarinssonab966862020-03-27 15:31:3860 --output-file /tmp/coverage_report/report.xml \
Yun Liu435bb172019-05-17 21:44:3461 --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 \
Finnur Thorarinssonab966862020-03-27 15:31:3870 --output-file /tmp/coverage_report/report.csv \
Yun Liu435bb172019-05-17 21:44:3471 --coverage-dir /tmp/coverage/ \
72 --sources-json-dir out/Debug/ \
73 ```