1. 程式人生 > >lcov 檢視程式碼覆蓋率 .hpp不能覆蓋問題

lcov 檢視程式碼覆蓋率 .hpp不能覆蓋問題

解決辦法:

https://stackoverflow.com/questions/9666800/getting-useful-gcov-results-for-header-only-libraries

I'm also using GCov to check test coverage (Tests written with Google Test framework), additionally I use the Eclipse GCov integration plugin or the LCov tool to generate easy to inspect views of the test coverage results. The raw GCov output is too hard to use :-(.

If you have header only template libraries, you also need to instrument (using G++ flag --coverage) your testing classes that instantiate the template classes and template member functions to see reasonable GCov outputs for these.

With the mentioned tools it's easy to spot template code that wasn't instantiated with the test cases at all, since it has NO annotations.

I have setup a sample and copied the LCov output to a DropBox link you can inspect.

Sample code (TemplateSampleTest.cpp is instrumented using the g++--coverageoption):


#include<string>#include"gtest/gtest.h"#include"TemplateSample.hpp"classTemplateSampleTest:public::testing::Test{public:TemplateSampleTest
(): templateSample(5){}protected:TemplateSample<int> templateSample;private:}; TEST_F(TemplateSampleTest,doSomethingPath1){ EXPECT_EQ(1,templateSample.doSomething(TemplateSample<int>::Path1));} TEST_F(TemplateSampleTest,doSomethingPath2){ EXPECT_EQ(2,templateSample.doSomething(TemplateSample<int>::Path2));} TEST_F(TemplateSampleTest,returnRefParam){ std::string stringValue ="Hello"; EXPECT_EQ(stringValue,templateSample.returnRefParam(stringValue));} TEST_F(TemplateSampleTest,doSomethingElse){ std::string stringValue ="Hello";long value = templateSample.doSomethingElse<std::string,long>(stringValue); EXPECT_EQ(5,value);}