blob: 36e85cd55ec8d80c3524b18716c6984e445b1575 [file] [log] [blame] [view]
Yuke Liao8bc00852019-01-29 19:22:391# Code Coverage in Gerrit
2
Yuke Liao0e5a20e2019-06-17 19:52:573Tests are critical because they find bugs and regressions, enforce better
4designs and make code easier to maintain. **Code coverage helps you ensure your
5tests are thorough**.
Yuke Liao8bc00852019-01-29 19:22:396
Yuke Liao0e5a20e2019-06-17 19:52:577Chromium CLs can show a line-by-line breakdown of test coverage. **You can use
Yuke Liao78aa43b2019-08-29 16:58:178it to ensure you only submit well-tested code**.
Yuke Liao0e5a20e2019-06-17 19:52:579
Yuke Liao78aa43b2019-08-29 16:58:1710To see code coverage for a Chromium CL, **trigger a CQ dry run**, and once the
Yuke Liaoa085b982019-10-24 21:37:1111builds finish and code coverage data is processed successfully, **look at the
12change view to see absolute and incremental code coverage percentages**:
13
14![code_coverage_percentages]
15
16Absolute coverage percentage is the percentage of lines covered by tests
17out of **all the lines** in the file, while incremental coverage percentage only
Prakharc7ae92e2023-09-20 09:34:5218accounts for **newly added or modified lines**. Both these coverage metrics, are further
19classified 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 Liaoa085b982019-10-24 21:37:1120
21To further dig into specific lines that are not covered by tests, **look at the
dpapad3eef9482023-11-08 19:13:4122right column of the side by side diff view, and specifically notice the
23background color of each line number**, where a light orange color indicates
24missing coverage and a light blue color indicates existing coverage. Moreover
25hovering over the line number shows an informative tooltip with
26"Not covered by tests" or "Covered by tests" text respectively. It only shows
27All Tests Coverage right now
Yuke Liao8bc00852019-01-29 19:22:3928
Yuke Liao0e5a20e2019-06-17 19:52:5729![code_coverage_annotations]
30
Yuke Liao78aa43b2019-08-29 16:58:1731**Code coverage data is shared between patchsets that are commit-message-edit or
32trivial-rebase away**, however, if a newly uploaded patchset has
33non-trivial code change, a new CQ dry run must be triggered before coverage data
34shows up again.
35
Prakharc7ae92e2023-09-20 09:34:5236The code coverage tool supports coverage for C/C++, JAVA and Javascript on all major platforms(Linux, MacOS, Windows, Android, iOS and ChromeOS)
Yuke Liao6e57a8a2019-11-12 22:23:4937
Prakharc7ae92e2023-09-20 09:34:5238## CLs Blocked Due to Low Coverage
39For 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.
40![low_coverage_message]
41
42Once 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.
43
Ashley Newsona0b1f81d2024-06-12 10:22:5444Tests themselves, as well as test-only files, are generally excluded from coverage checks based on their path or filename. If you are getting coverage warnings for test-related files themselves, check whether the files end in "test" or "tests" (for example, "SomethingTest.java" or "something_unittests.cc") or that their path contains a directory named exactly "test", "tests", or "testing". There is no manual list to which files can be added for long-term exclusion.
45
Prakhard4ac7f62024-01-16 21:19:1246Devs 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. This should follow certain formatting constraints which are mentioned below
47
48### Mention the Bypass Category
49
50The `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)
Prakharc7ae92e2023-09-20 09:34:5251
52Available category choices are:
53* **TRIVIAL_CHANGE**: CL contains mostly minor changes e.g. renaming, file moves, logging statements, simple interface definitions etc.
54* **TESTS_ARE_DISABLED**: Corresponding tests exist, but they are currently disabled.
55* **TESTS_IN_SEPARATE_CL**: Developer plan to write tests in a separate CL (Should not be exercised often as per best practices)
56* **HARD_TO_TEST**: The code under consideration is hard to test. For e.g. Interfaces with system, real hardware etc.
57* **COVERAGE_UNDERREPORTED**: To be used when the developer thinks that tests exist, but corresponding coverage is missing.
58* **LARGE_SCALE_REFACTOR**: The current change is part of a large scale refactor. Should explain why the refactor shouldn't have tests.
59* **EXPERIMENTAL_CODE**: The current code is experimental and unlikely to be released to users.
60* **OTHER**: None of the above categories are the right fit
61
62In 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
63![impropery_formatted_coverage_footer]
Yuke Liao0e5a20e2019-06-17 19:52:5764
Prakhard4ac7f62024-01-16 21:19:1265### No empty line after the footer
66In order for *Low-Coverage-Reason: reason* to work properly, it should occur after the last empty line in CL description, otherwise gerrit recognizes it as part of the commit message, rather than the footer i.e. Following would not work
67![empty_line_after_footer]
68
69Removing the empty line should fix it
70![no_empty_line_after_footer]
71
72### Be careful with long footer strings
73Either keep the footer message in one line i.e. do not add line breaks; or if you do, add whitespace on new footer lines, otherwise [gerrit doesnt parse them right]. e.g. a long footer message can be written as
74![long_footer]
75or
76![line_break_footer]
77
Yuke Liao8bc00852019-01-29 19:22:3978## Contacts
79
80### Reporting problems
81For any breakage report and feature requests, please [file a bug].
82
83### Mailing list
84For questions and general discussions, please join [code-coverage group].
85
86## FAQ
Yuke Liao8bc00852019-01-29 19:22:3987### Why is coverage not shown even though the try job finished successfully?
88
89There are several possible reasons:
Yuke Liao8bc00852019-01-29 19:22:3990* A particular source file/test may not be available on a particular project or
Prakhar28ffd9b12024-02-01 18:49:1291platform.
Yuke Liao8bc00852019-01-29 19:22:3992* There is a bug in the pipeline. Please [file a bug] to report the breakage.
93
94### How does it work?
95
96Please refer to [code_coverage.md] for how code coverage works in Chromium in
97general, and specifically, for per-CL coverage in Gerrit, the
98[clang_code_coverage_wrapper] is used to compile and instrument ONLY the source
Yuke Liao0e5a20e2019-06-17 19:52:5799files that are affected by the CL for the sake of performance and a
100[chromium-coverage Gerrit plugin] is used to display code coverage information
101in Gerrit.
Yuke Liao8bc00852019-01-29 19:22:39102
103
104[choose_tryjobs]: images/code_coverage_choose_tryjobs.png
Yuke Liao0e5a20e2019-06-17 19:52:57105[code_coverage_annotations]: images/code_coverage_annotations.png
Yuke Liaoa085b982019-10-24 21:37:11106[code_coverage_percentages]: images/code_coverage_percentages.png
Prakharc7ae92e2023-09-20 09:34:52107[low_coverage_message]: images/low_coverage_message.png
Prakhard4ac7f62024-01-16 21:19:12108[empty_line_after_footer]: images/empty_line_after_footer.png
109[no_empty_line_after_footer]: images/no_empty_line_after_footer.png
110[long_footer]: images/long_footer.png
111[line_break_footer]: images/line_break_footer.png
Prakharc7ae92e2023-09-20 09:34:52112[impropery_formatted_coverage_footer]: images/improperly_formatted_coverage_footer.png
Yuke Liao03c644072019-07-30 18:33:40113[file a bug]: https://bugs.chromium.org/p/chromium/issues/entry?components=Infra%3ETest%3ECodeCoverage
Yuke Liao8bc00852019-01-29 19:22:39114[code-coverage group]: https://groups.google.com/a/chromium.org/forum/#!forum/code-coverage
115[code_coverage.md]: code_coverage.md
John Palmer046f9872021-05-24 01:24:56116[clang_code_coverage_wrapper]: https://chromium.googlesource.com/chromium/src/+/main/docs/clang_code_coverage_wrapper.md
Yuke Liao8bc00852019-01-29 19:22:39117[chromium-coverage Gerrit plugin]: https://chromium.googlesource.com/infra/gerrit-plugins/code-coverage/
Prakhard4ac7f62024-01-16 21:19:12118[gerrit doesnt parse them right]: https://bugs.chromium.org/p/chromium/issues/detail?id=1459714#c9