Search code examples
lcov

lcov ignores files not included in unit test project


My project is being cross-compiled for an ARM target. However my unit tests run on the development host for speed. So naturally I cannot unit-test some files.

I recently set up code coverage measurement with gcov/lcov. It is completely ignoring the untested files, so my coverage is near 100 % although I know that it should be much lower.

I read about lcov's --capture --initial options, but I think I cannot run them on the source files that are not included in the unit test project.

How can I include the untested files with 0 % coverage and the correct line count?


Solution

  • I found the following solution. It involves cross-compiling my main project twice, once for normal usage and a second time with coverage information.

    I take these steps to measure the coverage:

    • Build the main project as usual (build directory e.g. build-main).
    • Build the main project a second time with coverage information (build directory e.g. build-main-coverage).
    • Generate an initial coverage report on this build: lcov --capture --initial --directory build-main-coverage --output-file baseline-coverage.info
    • Build the unit test project with coverage information (build directory e.g. build-unit-test).
    • Run the unit tests.
    • Generate another coverage report on the unit test project: lcov --capture --directory build-unit-test --output-file coverage.info
    • Combine the two reports: lcov --add-tracefile baseline-coverage.info --add-tracefile coverage.info --output-file coverage.info