LCOV is generating coverage reports for only header files - c++

I am using lcov and genhtml to generate code coverage reports.
My unit test files only include the .h header files.
client_test.cpp
#include "gtest/gtest.h"
#include "../client.h"
...
lcov --capture --no-external --rc lcov_branch_coverage=1 --directory . --output-file unfiltered_coverage.info
I have noticed that the coverage report only contains coverage information for the files which were in the include section of my unit tests. Thus, the coverage report only contains the client.h file and not the client.cpp file.

That makes sense because the coverage report will attach results relative to where functions were declared, not where they were implemented.

Related

Generate native code coverage report from 2 sets of .gcda files and one set of .gcno files

I'm using lcov to generate code coverage report for native part of the product.
Using the command lcov --capture --directory <path_to_obj> --output-file coverage.info (path_to_obj - path to dir with all .gcno and .gcda files are) it's not a problem to generate report.
But, if I have several sets of (directories with) .gcda files and one set (a directory with) .gcno files.
Is it possible to generate the report as the files are? (not copying .gcno files to each folder with .gcda files)
I know that I can copy .gcno files to each folder with .gcda files, generate .info files and then merge them, but I want to avoid that.
From the man lcov, I can see that --directory parameter stands for "Use .da (as I understood .gcda files counts here) files in directory instead of kernel." Also, "Note that you may specify this option more than once."
But I didn't find similar parameters for specifying .gcno files location, is there such at all or they must(!) lay together?
I saw the question How to deal with .gcda and .gcno files in different directory by lcov & code coverage compare and saw a comment that .gcno and .gcda files should be generated at the same location, but in my case, they are not generated in one location (it's a correct behavior).

CMake: How to reuse the same test_main.cpp for each test

I would like to use the test framework Catch2 in a monorepo in which there will be many components, each with their own tests. I'd like to use CMake to define all build targets.
Catch2 provides a simple means to generate a common main() function that will run all tests linked into the application. The source file (which I am calling test_main.cpp) is just these two lines:
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
I'd like to have just one copy of test_main.cpp in my source tree, and refer to it from each component directory that has tests (which will end up being on the order of hundreds of directories).
I can see how to set up CMake to work if I duplicated the test_main.cpp so that a redundant copy existed in each directory with tests, but I haven't figured out how to have just one copy. Is there a simple trick I've overlooked?
Try integrating Catch2 as a CMake interface library in the following way:
Add the single header file catch.hpp and test_main.cpp to a directory of their own (e.g., Catch2) along with the following CMakeLists.txt:
add_library(catch INTERFACE)
target_include_directories(catch INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_sources(catch INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/test_main.cpp")
The in your outermost CMakeLists.txt include Catch2 with add_subdirectory:
add_subdirectory(Catch2)
You can then set up test executables anywhere in the project in the following way:
add_executable(
project_tests EXCLUDE_FROM_ALL
test_case1.cpp
test_case2.cpp
...
)
target_link_libraries(project_tests catch)
Because test_main.cpp has been added to catch as an interface source file, it will be compiled as part of the test executable.

Exclude the header files coverage while doing the unitests for .cpp files using lcov

I have generated the html code coverage for unittest. The problem is that it generates the code coverage for everything that is defined in the include preprocessor.
The codecoverage for the unittest is inside the unitTest link of the code coverage report. I only need the unitTest
I also remove everything else and keep the gcda and gcno files of the test file before generating the code coverage but it still doesnot help. The code that does this is :
find obj/ ! -name '*_unittests*' -type f -delete
The code coverage report is show below :
I tried to add LCOV_EXCL_START and LCOV_EXCL_STOP at the beginning and end of include preprocessors as follows but it still doesnot help.
LCOV_EXCL_START
#include <inttypes.h>
#include "../src/generators/buffersss.h"
#include "gtest/gtest.h"
#include <getopt.h>
LCOV_EXCL_STOP
Test code goes here.....
What am i doing wrong here?
The following code solved the issue
lcov --remove coverage.info '/usr/include/*' 'src/*' -o filtered_coverage.info

How to generate .h header file using makefile and use it in same makefile for compilation of source?

I want to generate a header file abc.h using makefile.
And, include this header file in abc.cpp file, which has to be built from same makefile.
I am facing issue that it is not generating .h file before compiling .cpp file. So, i am getting an fatal error for header file not found.
e.g:
//abc.h
#define VAR "My Name"
//abc.cpp
#include "abc.h"
So, question is how to write makefile where it generates header file before compiling source file so that i don't get header file inclusion error?
I have created makefile like below but it's not generating file before any other compilation:
-include dummy
noinst_LTLIBRARIES = window.la
.PHONY: dummy
dummy:
#echo "#define VAR "MYNAME" > abc.h
window_la_SOURCES = \
../src/abc.cpp
window_la_CPPFLAGS = \
-I $(srcdir)/../src/
I want to generate abc.h in above makefile before my compilation begins for abc.cpp. So, that i don't get fatal error for header inclusion. Please suggest.
Makefiles are a simple way to organize code compilation.
You have to create the header file yourself.
how to write makefile where it generates header file before compiling source file so that i don't get header file inclusion error?
Here is a simple example (assuming that abc.cpp exists, compiles and has main()):
all: abc
abc.h: Makefile
#echo "Generating $#"
#echo "#define VAR \"MYNAME\"" > $#
abc.o: abc.h
abc.cpp: abc.h
abc: abc.o
By default build abc.
The target abc.h is generated by the shell script inside the Makefile and thus depends on the Makefile.
Tell make that the abc.cpp requires the abc.h.
Tell make that abc.o should be rebuild if abc.h changes.
Tell make to build the abc from the abc.o.
Please suggest.
Code generation in the GNU make-based systems is hard and error prone. Though simple example as above seem trivial, on larger projects you might run into various problems for which GNU make offers literally no help in diagnosing or debugging.
My suggestion would be, unless you are prepared to read through the whole of GNU make manual, is to avoid that.
Also, for the cases when you just need a preprocessor define, code generation is really an overkill: most IDEs and build systems already provide way to add preprocessor defines to the project. And many many things in the C/C++ could be reduced to the preprocessor defines.

How do I tell lcov where to find my .cpp & .h files?

My source and build tree looks like this (see Makefile to put object files from source files different directories into a single, separate directory?) after a make (which builds and runs FooAndBarTests):
src
- Foo.cpp
- Bar.cpp
inc
- Foo.h
- Bar.h
test
- FooTest.cpp
- BarTest.cpp
- Main.cpp
- Makefile
- obj
- Foo.gcda
- Foo.gcno
- Bar.gcda
- Bar.gcno
- FooAndBarTests
UnitTest++
- libUnitTest++.a
- src
- ...
I can then produce .gcov files in the test directory by running gcov -o obj/ ../src/Foo.cpp and gcov -o obj/ ../src/Bar.cpp.
But if I run lcov -d obj/ -c -o FooAndBarTests.lcov I get:
Capturing coverage data from obj/
Found gcov version: 4.2.1
Scanning obj/ for .gcda files ...
Found 4 data files in obj/
Processing Foo.gcda
../src/Foo.cpp:cannot open source file
Processing FooTest.gcda
FooTest.cpp:cannot open source file
../inc/Foo.h:cannot open source file
../UnitTest++/src/Checks.h:cannot open source file
...
And then when I run genhtml FooAndBarTests.lcov I get:
Reading data file FooAndBarTests.lcov
Found 45 entries.
Found common filename prefix "/Users/dspitzer/FooAndBar/test"
Writing .css and .png files.
Generating output.
Processing file UnitTest++/src/Checks.h
genhtml: ERROR: cannot open /Users/dspitzer/FooAndBar/test/UnitTest++/src/Checks.h for reading!
How do I tell lcov and genhtml where the .cpp and .h files are?
Use the -b option to lcov. The -b option specifies code base.

Resources