Coverage data is collected per compilation unit. In your example, you have one compilation unit for the test1.cpp
file. The compiler will generate a .gcno file that allows other tools to associate coverage data with lines in the files in this compilation unit. But since only header1.h
was included, there will not be any such information in the .gcno file about header2 and header3.
Coverage tools like gcov and gcovr are language-agnostic, and do not search your directories for files that might contain uncovered source code. If you want the coverage report to know about the files, you must include the file in a compilation unit that you compile with --coverage
. Even then, you will note that GCC ignores inline functions that were not called. You can only get coverage data when machine code was generated for that code.
This is a fundamental limitation of the gcov coverage mechanism, and means that you have to take care when interpreting coverage metrics for header files. Do not mistake a high coverage percentage to mean that everything that could be covered also was covered – it merely means that the machine code generated for code in that file was thoroughly exercised.