xref: /aosp_15_r20/external/bazelbuild-rules_python/docs/coverage.md (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1*60517a1eSAndroid Build Coastguard Worker# Setting up coverage
2*60517a1eSAndroid Build Coastguard Worker
3*60517a1eSAndroid Build Coastguard WorkerAs of Bazel 6, the Python toolchains and bootstrap logic supports providing
4*60517a1eSAndroid Build Coastguard Workercoverage information using the `coverage` library.
5*60517a1eSAndroid Build Coastguard Worker
6*60517a1eSAndroid Build Coastguard WorkerAs of `rules_python` version `0.18.1`, builtin coverage support can be enabled
7*60517a1eSAndroid Build Coastguard Workerwhen configuring toolchains.
8*60517a1eSAndroid Build Coastguard Worker
9*60517a1eSAndroid Build Coastguard Worker## Enabling `rules_python` coverage support
10*60517a1eSAndroid Build Coastguard Worker
11*60517a1eSAndroid Build Coastguard WorkerEnabling the coverage support bundled with `rules_python` just requires setting an
12*60517a1eSAndroid Build Coastguard Workerargument when registerting toolchains.
13*60517a1eSAndroid Build Coastguard Worker
14*60517a1eSAndroid Build Coastguard WorkerFor Bzlmod:
15*60517a1eSAndroid Build Coastguard Worker
16*60517a1eSAndroid Build Coastguard Worker```starlark
17*60517a1eSAndroid Build Coastguard Workerpython.toolchain(
18*60517a1eSAndroid Build Coastguard Worker    "@python3_9_toolchains//:all",
19*60517a1eSAndroid Build Coastguard Worker    configure_coverage_tool = True,
20*60517a1eSAndroid Build Coastguard Worker)
21*60517a1eSAndroid Build Coastguard Worker```
22*60517a1eSAndroid Build Coastguard Worker
23*60517a1eSAndroid Build Coastguard WorkerFor WORKSPACE configuration:
24*60517a1eSAndroid Build Coastguard Worker
25*60517a1eSAndroid Build Coastguard Worker```starlark
26*60517a1eSAndroid Build Coastguard Workerpython_register_toolchains(
27*60517a1eSAndroid Build Coastguard Worker   register_coverage_tool = True,
28*60517a1eSAndroid Build Coastguard Worker)
29*60517a1eSAndroid Build Coastguard Worker```
30*60517a1eSAndroid Build Coastguard Worker
31*60517a1eSAndroid Build Coastguard Worker:::{note}
32*60517a1eSAndroid Build Coastguard WorkerThis will implicitly add the version of `coverage` bundled with
33*60517a1eSAndroid Build Coastguard Worker`rules_python` to the dependencies of `py_test` rules when `bazel coverage` is
34*60517a1eSAndroid Build Coastguard Workerrun. If a target already transitively depends on a different version of
35*60517a1eSAndroid Build Coastguard Worker`coverage`, then behavior is undefined -- it is undefined which version comes
36*60517a1eSAndroid Build Coastguard Workerfirst in the import path. If you find yourself in this situation, then you'll
37*60517a1eSAndroid Build Coastguard Workerneed to manually configure coverage (see below).
38*60517a1eSAndroid Build Coastguard Worker:::
39*60517a1eSAndroid Build Coastguard Worker
40*60517a1eSAndroid Build Coastguard Worker## Manually configuring coverage
41*60517a1eSAndroid Build Coastguard Worker
42*60517a1eSAndroid Build Coastguard WorkerTo manually configure coverage support, you'll need to set the
43*60517a1eSAndroid Build Coastguard Worker`py_runtime.coverage_tool` attribute. This attribute is a target that specifies
44*60517a1eSAndroid Build Coastguard Workerthe coverage entry point file and, optionally, client libraries that are added
45*60517a1eSAndroid Build Coastguard Workerto `py_test` targets. Typically, this would be a `filegroup` that looked like:
46*60517a1eSAndroid Build Coastguard Worker
47*60517a1eSAndroid Build Coastguard Worker```starlark
48*60517a1eSAndroid Build Coastguard Workerfilegroup(
49*60517a1eSAndroid Build Coastguard Worker  name = "coverage",
50*60517a1eSAndroid Build Coastguard Worker  srcs = ["coverage_main.py"],
51*60517a1eSAndroid Build Coastguard Worker  data = ["coverage_lib1.py", ...]
52*60517a1eSAndroid Build Coastguard Worker)
53*60517a1eSAndroid Build Coastguard Worker```
54*60517a1eSAndroid Build Coastguard Worker
55*60517a1eSAndroid Build Coastguard WorkerUsing `filegroup` isn't required, nor are including client libraries. The
56*60517a1eSAndroid Build Coastguard Workerimportant behaviors of the target are:
57*60517a1eSAndroid Build Coastguard Worker
58*60517a1eSAndroid Build Coastguard Worker*   It provides a single output file OR it provides an executable output; this
59*60517a1eSAndroid Build Coastguard Worker    output is treated as the coverage entry point.
60*60517a1eSAndroid Build Coastguard Worker*   If it provides runfiles, then `runfiles.files` are included into `py_test`.
61