Search code examples
googletestgcovlcov

gcov/lcov + googletest create an artificially low branch coverage report


First, I am well aware of the "hidden branch" problem caused by throws/exceptions. This is not that.

What I am observing is:

  1. My test framework (googletest) has testing macros (EXPECT_TRUE for example).
  2. I write passing tests using the macros
  3. Measuring branch coverage now asymptotes at 50% because I have not evaluated that test in both a passing and a failing condition...

Consider the following:

TEST (MyTests, ContrivedTest)
{
    EXPECT_TRUE(function_that_always_returns_true());
}

Now assuming that I have every line and every branch perfectly covered in function_that_always_returns_true(), this branch coverage report will asymptote at 50% (because gcov does not observe line 3 evaluating in a failing condition, intentionally)

The only idea that I've had around this issue is that I could exclude the evaluation macros with something like LCOV_EXCL_BR_LINE, but this feels both un-ergonomic and hacky.

TEST (MyTests, ContrivedTest)
{
    bool my_value = function_that_always_returns_true();
    EXPECT_TRUE(my_value); //LCOV_EXCL_BR_LINE
}

This cannot be a niche problem, and I have to believe that people successfully use googletest with lcov/gcov. What do people do to get around this limitation?


Solution

  • After looking for far too long, I realized that all the testing calls I want to filter out are of the pattern EXPECT_*. So simply adding:

    lcov_excl_br_line=LCOV_EXCL_BR_LINE|EXPECT_*
    

    to my lcovrc solved my problem