Yuke Liao | 8bc0085 | 2019-01-29 19:22:39 | [diff] [blame] | 1 | # Code Coverage in Gerrit |
| 2 | |
Yuke Liao | 0e5a20e | 2019-06-17 19:52:57 | [diff] [blame] | 3 | Tests are critical because they find bugs and regressions, enforce better |
| 4 | designs and make code easier to maintain. **Code coverage helps you ensure your |
| 5 | tests are thorough**. |
Yuke Liao | 8bc0085 | 2019-01-29 19:22:39 | [diff] [blame] | 6 | |
Yuke Liao | 0e5a20e | 2019-06-17 19:52:57 | [diff] [blame] | 7 | Chromium CLs can show a line-by-line breakdown of test coverage. **You can use |
Yuke Liao | 78aa43b | 2019-08-29 16:58:17 | [diff] [blame] | 8 | it to ensure you only submit well-tested code**. |
Yuke Liao | 0e5a20e | 2019-06-17 19:52:57 | [diff] [blame] | 9 | |
Yuke Liao | 78aa43b | 2019-08-29 16:58:17 | [diff] [blame] | 10 | To see code coverage for a Chromium CL, **trigger a CQ dry run**, and once the |
Yuke Liao | a085b98 | 2019-10-24 21:37:11 | [diff] [blame] | 11 | builds finish and code coverage data is processed successfully, **look at the |
| 12 | change view to see absolute and incremental code coverage percentages**: |
| 13 | |
| 14 | ![code_coverage_percentages] |
| 15 | |
| 16 | Absolute coverage percentage is the percentage of lines covered by tests |
| 17 | out of **all the lines** in the file, while incremental coverage percentage only |
Prakhar | c7ae92e | 2023-09-20 09:34:52 | [diff] [blame^] | 18 | accounts for **newly added or modified lines**. Both these coverage metrics, are further |
| 19 | classified into Unit Tests coverage(coverage from just unit tests) and All Tests coverage(covererd by all tests running in CQ, including unit tests). All Tests coverage is a superset of Unit Tests coverage. |
Yuke Liao | a085b98 | 2019-10-24 21:37:11 | [diff] [blame] | 20 | |
| 21 | To further dig into specific lines that are not covered by tests, **look at the |
Prakhar | c7ae92e | 2023-09-20 09:34:52 | [diff] [blame^] | 22 | right column of the side by side diff view**. It only shows All Tests Coverage right now |
Yuke Liao | 8bc0085 | 2019-01-29 19:22:39 | [diff] [blame] | 23 | |
Yuke Liao | 0e5a20e | 2019-06-17 19:52:57 | [diff] [blame] | 24 | ![code_coverage_annotations] |
| 25 | |
Yuke Liao | 78aa43b | 2019-08-29 16:58:17 | [diff] [blame] | 26 | **Code coverage data is shared between patchsets that are commit-message-edit or |
| 27 | trivial-rebase away**, however, if a newly uploaded patchset has |
| 28 | non-trivial code change, a new CQ dry run must be triggered before coverage data |
| 29 | shows up again. |
| 30 | |
Prakhar | c7ae92e | 2023-09-20 09:34:52 | [diff] [blame^] | 31 | The code coverage tool supports coverage for C/C++, JAVA and Javascript on all major platforms(Linux, MacOS, Windows, Android, iOS and ChromeOS) |
Yuke Liao | 6e57a8a | 2019-11-12 22:23:49 | [diff] [blame] | 32 | |
Prakhar | c7ae92e | 2023-09-20 09:34:52 | [diff] [blame^] | 33 | ## CLs Blocked Due to Low Coverage |
| 34 | For some teams in Chrome, we have turned on a coverage check, which blocks a CL from submission if the incremental coverage is below a preset threshold(default = 70%). CLs with insufficient test coverage have a `CodeCoverage-1` label added to them, which prevents them from being submitted. Also, a descriptive message is added to the CL, notifying developer of why the CL was blocked, and how to resolve it. |
| 35 | ![low_coverage_message] |
| 36 | |
| 37 | Once the tests are added, another run of coverage builders (through CQ+1 or CQ+2) changes the label to `CodeCoverage+1`, allowing CLs to proceed with submission. |
| 38 | |
| 39 | Devs can also choose to bypass this block, in case they think they are being unfairly punished. They can do so by adding a *Low-Coverage-Reason: reason* footer to the change description. The `reason` string should mention the category the bypass reason belongs to. For e.g. *Low-Coverage-Reason: TRIVIAL_CHANGE This change contains only minor cosmetic changes.* (TRIVIAL_CHANGE is the category) |
| 40 | |
| 41 | Available category choices are: |
| 42 | * **TRIVIAL_CHANGE**: CL contains mostly minor changes e.g. renaming, file moves, logging statements, simple interface definitions etc. |
| 43 | * **TESTS_ARE_DISABLED**: Corresponding tests exist, but they are currently disabled. |
| 44 | * **TESTS_IN_SEPARATE_CL**: Developer plan to write tests in a separate CL (Should not be exercised often as per best practices) |
| 45 | * **HARD_TO_TEST**: The code under consideration is hard to test. For e.g. Interfaces with system, real hardware etc. |
| 46 | * **COVERAGE_UNDERREPORTED**: To be used when the developer thinks that tests exist, but corresponding coverage is missing. |
| 47 | * **LARGE_SCALE_REFACTOR**: The current change is part of a large scale refactor. Should explain why the refactor shouldn't have tests. |
| 48 | * **EXPERIMENTAL_CODE**: The current code is experimental and unlikely to be released to users. |
| 49 | * **OTHER**: None of the above categories are the right fit |
| 50 | |
| 51 | In case the developer doesn't specify the coverage category as prescribed, a warning will be shown in the UI, with details on how to fix |
| 52 | ![impropery_formatted_coverage_footer] |
Yuke Liao | 0e5a20e | 2019-06-17 19:52:57 | [diff] [blame] | 53 | |
Yuke Liao | 8bc0085 | 2019-01-29 19:22:39 | [diff] [blame] | 54 | ## Contacts |
| 55 | |
| 56 | ### Reporting problems |
| 57 | For any breakage report and feature requests, please [file a bug]. |
| 58 | |
| 59 | ### Mailing list |
| 60 | For questions and general discussions, please join [code-coverage group]. |
| 61 | |
| 62 | ## FAQ |
Yuke Liao | 8bc0085 | 2019-01-29 19:22:39 | [diff] [blame] | 63 | ### Why is coverage not shown even though the try job finished successfully? |
| 64 | |
| 65 | There are several possible reasons: |
Yuke Liao | 8bc0085 | 2019-01-29 19:22:39 | [diff] [blame] | 66 | * A particular source file/test may not be available on a particular project or |
| 67 | platform. As of now, only `chromium/src` project and `Linux` platform is |
| 68 | supported. |
| 69 | * There is a bug in the pipeline. Please [file a bug] to report the breakage. |
| 70 | |
| 71 | ### How does it work? |
| 72 | |
| 73 | Please refer to [code_coverage.md] for how code coverage works in Chromium in |
| 74 | general, and specifically, for per-CL coverage in Gerrit, the |
| 75 | [clang_code_coverage_wrapper] is used to compile and instrument ONLY the source |
Yuke Liao | 0e5a20e | 2019-06-17 19:52:57 | [diff] [blame] | 76 | files that are affected by the CL for the sake of performance and a |
| 77 | [chromium-coverage Gerrit plugin] is used to display code coverage information |
| 78 | in Gerrit. |
Yuke Liao | 8bc0085 | 2019-01-29 19:22:39 | [diff] [blame] | 79 | |
| 80 | |
| 81 | [choose_tryjobs]: images/code_coverage_choose_tryjobs.png |
Yuke Liao | 0e5a20e | 2019-06-17 19:52:57 | [diff] [blame] | 82 | [code_coverage_annotations]: images/code_coverage_annotations.png |
Yuke Liao | a085b98 | 2019-10-24 21:37:11 | [diff] [blame] | 83 | [code_coverage_percentages]: images/code_coverage_percentages.png |
Prakhar | c7ae92e | 2023-09-20 09:34:52 | [diff] [blame^] | 84 | [low_coverage_message]: images/low_coverage_message.png |
| 85 | [impropery_formatted_coverage_footer]: images/improperly_formatted_coverage_footer.png |
Yuke Liao | 03c64407 | 2019-07-30 18:33:40 | [diff] [blame] | 86 | [file a bug]: https://bugs.chromium.org/p/chromium/issues/entry?components=Infra%3ETest%3ECodeCoverage |
Yuke Liao | 8bc0085 | 2019-01-29 19:22:39 | [diff] [blame] | 87 | [code-coverage group]: https://groups.google.com/a/chromium.org/forum/#!forum/code-coverage |
| 88 | [code_coverage.md]: code_coverage.md |
John Palmer | 046f987 | 2021-05-24 01:24:56 | [diff] [blame] | 89 | [clang_code_coverage_wrapper]: https://chromium.googlesource.com/chromium/src/+/main/docs/clang_code_coverage_wrapper.md |
Yuke Liao | 8bc0085 | 2019-01-29 19:22:39 | [diff] [blame] | 90 | [chromium-coverage Gerrit plugin]: https://chromium.googlesource.com/infra/gerrit-plugins/code-coverage/ |