Coverage

The coverage macro generates coverage reports from previously collected coverage data.

Dependencies

The required tool depends on the coverage workflow:

  • pycoverage() requires coverage.

  • ccoverage() requires gcovr and a compatible gcov tool.

For GCC builds, gcovr uses gcov by default. The gcov version should match the GCC version used to compile the instrumented code. Clang-based builds can use the compatible llvm-cov gcov mode instead.

Features

  • Python coverage processing

  • optional cleanup of Python coverage data directories

  • C/C++ coverage processing with gcovr

  • Cobertura XML generation

  • HTML coverage reports

  • cikit data updates

  • artifact registration

  • coverage badges

  • RST report generation

  • timeline integration

Python coverage

Parallel coverage data generated by the pytest macro can be combined and rendered with:

from pathlib import Path

from cikit.macros import coverage

coverage.pycoverage(env, Path("build/coverage"))
coverage.badge(env)
coverage.write_rst(env)

Cleaning Python coverage data

clean_pycoverage() removes the entire specified Python coverage data directory, including its files and subdirectories. This is useful at the beginning of a new coverage collection cycle to prevent stale measurements from being combined with current results. The directory is resolved relative to the project root when given as a relative path. If it does not exist, cleanup has no effect.

from pathlib import Path

from cikit.macros import coverage, pytest

coverage_dir = Path("build/coverage")
coverage.clean_pycoverage(env, coverage_dir)
pytest.pytest(env, Path("tests/unittests"), test_alias="unittests",
              coverage_dir=coverage_dir)
pytest.pytest(env, Path("tests/systemtests"), test_alias="systemtests",
              coverage_dir=coverage_dir)
coverage.pycoverage(env, coverage_dir)

Clean the directory only before the first test run when multiple suites collect coverage into the same location. The pytest macro recreates the directory when coverage collection starts. Do not point cleanup at a directory containing other files that must be retained. This function is specific to Python coverage.

The built-in pytest_init command exposes the same cleanup operation; see Built-in CI Commands for CLI usage.

C/C++ coverage

For C or C++ projects, ccoverage() processes gcovr coverage data below the project CMake build root by default. This allows coverage data from multiple instrumented build subdirectories to contribute to one report:

from cikit.macros import coverage

coverage.ccoverage(env)

The default build root is the same PROJECT_ROOT/build location used by the CMake macro. A specific coverage directory can still be supplied explicitly when only one build tree should be evaluated:

from pathlib import Path

coverage.ccoverage(env, Path("build/unit-coverage"))

Generated HTML coverage output is registered as a folder artifact. Generated RST pages refer to registered artifacts rather than depending on the physical public output layout.

Artifacts

Both coverage workflows register Cobertura XML as an XUNIT artifact and HTML output as a FOLDER artifact with index.html as its entry point. badge() and write_rst() create the BADGE and RST artifacts, respectively.