In beman.utf_view, we get code coverage using gcov
and Coveralls.
utf_view
currently has three CI jobs, configured using GitHub Actions: gcc-linux
, clang-linux
, and msvc-windows
. Coverage is currently measured as part of the gcc-linux
CI job.
To ensure that running the unit tests generates gcov
data, the gcc-linux
CI job sets -DCMAKE_CXX_FLAGS='-coverage'
when compiling.
It then runs the unit tests using ctest
.
Note that all the functionality utf_view
proposes for standardization is constexpr
, so most of the unit tests are implemented as constexpr
functions returning a bool
. These unit tests can be checked by invoking them inside a static_assert
, but to ensure we get coverage data, utf_view
makes sure to also execute the test functions at runtime.
After the unit tests have generated the raw gcov
coverage data, an additional lcov
step in the CI job consolidates the gcov
data into a .info
file:
lcov --directory ./build --capture --output-file ./build/coverage_all.info
It then removes any coverage data originating from the standard library, thirdparty dependencies, or the unit tests themselves:
lcov --remove ./build/coverage_all.info -o ./build/coverage.info '/usr/include/*' "$PWD/tests/beman/utf_view/*" "$PWD/build/_deps/*"
Finally, the filtered test coverage file is uploaded to Coveralls using their GitHub Action.
Coveralls provides a page for each source file showing covered lines in green and uncovered lines in red. An example is here, although you need to log in to Coveralls to see it (otherwise it will say “Source Not Available”): bemanproject/utf_view | Build 11933026153 | include/beman/utf_view/to_utf_view.hpp | Coveralls - Test Coverage History & Statistics
Coveralls automatically leaves comments on pull requests with coverage reports: Add test random access iterator and corresponding concept check for code unit view by ednolan · Pull Request #14 · bemanproject/utf_view · GitHub
And it provides a coverage badge that can be added to the README file.
This approach does not require any modification to the CMake, since users can simply provide -coverage
using -DCMAKE_CXX_FLAGS
.
To my understanding, although my current implementation in utf_view uses Docker, this isn’t a hard requirement, since lcov can be installed onto a GitHub runner image.
It doesn’t cost anything because Coveralls only charges for private repos, stating “Coveralls will always be free for open source.”
I’m interested in feedback about this approach-- if we think we might want to roll it out to other repositories, I can create a pull request for Optional26 to add coverage to that repository as a first step.