After failing to get coverage with-cmake I set up a minimalistic project to see if I can get coverage working that way. It's derived from using-gtest-without-cmake
It has a src
folder with a header and source file in it.
QuickMaths.hpp :
#include <cstdint>
using size_t = std::size_t;
size_t
multiply(size_t a, size_t b);
inline size_t
add(size_t a, size_t b)
{
return a + b;
}
class QuickMaths
{
public:
size_t operator*() const;
friend QuickMaths operator+(QuickMaths const&, QuickMaths const&);
QuickMaths(size_t x);
private:
size_t x;
};
QuickMaths.cpp:
#include "QuickMaths.hpp"
size_t
multiply(size_t a, size_t b)
{
return a * b;
}
size_t
QuickMaths::operator*() const
{
return x;
}
QuickMaths::QuickMaths(size_t x)
: x(x)
{}
QuickMaths
operator+(QuickMaths const& a, QuickMaths const& b)
{
return a.x + b.x;
}
And a test folder with QuickMaths.cpp :
#include <gtest/gtest.h>
#include <QuickMaths.hpp>
TEST(AddTest, shouldAdd)
{
EXPECT_EQ(add(1UL, 1UL), 2UL);
}
TEST(MultiplyTest, shouldMultiply)
{
EXPECT_EQ(multiply(2UL, 4UL), 8UL);
}
TEST(QuickMathTest, haveValue)
{
auto v = QuickMaths{ 4UL };
EXPECT_EQ(*v, 4UL);
}
and main.cpp :
#include <gtest/gtest.h>
int
main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
- I create a
build
folder andcd
into it, then compile usingg++ --coverage -O0 ../src/QuickMaths.cpp ../test/*.cpp -I../src/ -pthread -lgtest -lgtest_main -lgcov
- I run
./a.out
=> output shows tests being run and passing
Lastly I run gcovr -r ../ .
and get the following output:
------------------------------------------------------------------------------
GCC Code Coverage Report
Directory: ../
------------------------------------------------------------------------------
File Lines Exec Cover Missing
------------------------------------------------------------------------------
src/QuickMaths.hpp 2 0 0% 9,11
test/QuickMaths.cpp 7 0 0% 5,7,10,12,15,17-18
test/main.cpp 3 3 100%
------------------------------------------------------------------------------
TOTAL 12 3 25%
------------------------------------------------------------------------------
So it's visible that the gtest setup situated in main is being picked up, but the test cases themselves as well as the code from the src
directory is not picked up as executed.