I am using gcov/lcov for coverage analysis of googletest unit tests.
One recurring problem is that the coverage report shows uncovered lines in the test code for some googletest macros when the macro is spread over several lines.
I know gov/lcov cannot be more accurate than a single line, but I am puzzled by the behaviour that I see. Can someone explain this? Minimal example:
#include <gtest/gtest.h>
TEST(coverage,incomplete)
{
// Every second line in every invocation here will show up as uncovered:
EXPECT_NO_THROW(40 +
2);
EXPECT_NO_THROW(40 + 2
);
EXPECT_NO_THROW(40 + 2)
;
}
TEST(coverage,complete)
{
// This test does not show uncovered lines
EXPECT_NO_THROW(40 + 2);
EXPECT_EQ(40
+
2
, // even though this is spread over several lines
42
)
;
}
How coverage analysis was done:
g++-4.8 -Igtest/googletest/include/ --coverage -o coverage_macropp coverage_macropp.cpp gtest/googletest/make/gtest_main.a -pthread
./coverage_macropp
lcov --capture --directory . --output-file coverage.info
genhtml --demangle-cpp coverage.info --output-directory coverage
The coverage analysis in the web browser will then show lines 7, 9, and 11 as uncovered:
Line data Source code
1 : #include <gtest/gtest.h>
2 :
3 5 : TEST(coverage,incomplete)
4 : {
5 : // Every second line in every invocation here will show up as uncovered:
6 1 : EXPECT_NO_THROW(40 +
7 0 : 2);
8 1 : EXPECT_NO_THROW(40 + 2
9 0 : );
10 1 : EXPECT_NO_THROW(40 + 2)
11 0 : ;
12 1 : }
13 :
14 5 : TEST(coverage,complete)
15 : {
16 : // This test does not show uncovered lines
17 1 : EXPECT_NO_THROW(40 + 2);
18 1 : EXPECT_EQ(40
19 : +
20 : 2
21 : , // even though this is spread over several lines
22 : 42
23 : )
24 1 : ;
25 4 : }
Why? And why is the EXPECT_EQ macro not affected?