xref: /aosp_15_r20/external/coreboot/Documentation/technotes/2021-05-code-coverage.md (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1# Unit Test Code Coverage
2
3Code coverage for the coreboot unit tests allows us to see what lines of
4code in the coreboot library are covered by unit tests, and allows a test
5author to see where they need to add test cases for additional coverage.
6
7Code coverage requires `lcov`; install the tool if necessary by
8`sudo apt install lcov` or the equivalent for your system.
9
10Enable code coverage in your unit test build by setting the environment
11variable `COV` to 1; either `export COV=1` in your shell, or add it to your
12`make` command, e.g. `COV=1 make unit-tests`.
13
14The build output directory is either `build/tests` or `build/coverage`,
15depending on whether `COV=1` is set in the environment.
16
17All of the unit test targets are available with and without `COV=1`
18* `clean-unit-tests`
19* `build-unit-tests`
20* `run-unit-tests`
21* `unit-tests` (which is just `build-unit-tests` followed by `run-unit-tests`)
22
23There are two new `make` targets:
24* `coverage-report` generates a code coverage report from all of the
25GCOV data (`*.gcda` and `*.gcno` files) in the build directory. To view the
26coverage report, open `build/coverage/coverage_reports/index.html` in your web
27browser.
28* `clean-coverage-report` deletes just the coverage report.
29
30The `coverage-report` and `clean-coverage-report` targets automatically set
31`COV=1` if it is not already set in the environment.
32
33
34## Examples
35
36`COV=1 make unit-tests coverage-report` builds all of the unit tests with code
37coverage, runs the unit tests, and generates the code coverage report.
38
39`COV=1 make build-unit-tests` builds all of the unit tests with code coverage.
40
41`COV=1 make run-unit-tests` runs the unit tests, building them with code
42coverage if they are out-of-date.
43
44`COV=1 make coverage-report` creates the code coverage report. This
45target does not explicitly depend on the tests being built and run; it gathers
46the code coverage data from the output directory, which it assumes already
47exists.
48
49`COV=1 make tests/lib/uuid-test coverage-report` builds the uuid test
50with code coverage, runs it, and generates a code coverage report just for
51that test.
52
53As a demonstration that building with and without coverage uses different
54output directories:
551. `make build-unit-tests` builds unit tests without code coverage into
56`build/tests`.
572. `COV=1 make clean-unit-tests` cleans `build/coverage`
583. `make build-unit-tests` doesn't need to build anything in `build/tests`,
59because those files weren't affected by the previous `clean-unit-tests`.
60