Jan Schatz
Jan Schatz

Reputation: 365

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?

Upvotes: 0

Views: 184

Answers (2)

Jan Schatz
Jan Schatz

Reputation: 365

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

Upvotes: 0

Henry Cox
Henry Cox

Reputation: 91

The --initial and/or --all option tells lcov --capture to look at .gcno files (compile-time coverage data) that do not also have a .gcda file (runtime coverage data). There will be no runtime data if the corresponding compilation unit is not used in your executable. As you point out: using either of those options will tell the tool to look at both used and unused sources.

Upvotes: 0

Related Questions