また、GCovを使用してテストカバレッジ(Google Testフレームワークで記述されたテスト)をチェックしています。さらに、Eclipse GCov統合プラグインまたはLCovツールを使用して、テストカバレッジ結果のビューを簡単に検査できます。生のGCov出力は使用するのが難しすぎます:-(。
ヘッダーのみのテンプレートライブラリがある場合は、テンプレートクラスとテンプレートメンバー関数をインスタンス化するテストクラスを(G ++フラグ--coverageを使用して)インストルメント化して、これらの適切なGCov出力を確認する必要もあります。
前述のツールを使用すると、アノテーションがないため、テストケースでインスタンス化されなかったテンプレートコードを簡単に見つけることができます。
サンプルをセットアップし、LCov出力を検査可能なDropBoxリンクにコピーしました。
サンプルコード(TemplateSampleTest.cppはg ++--coverage
オプションを使用してインストルメント化されています):
TemplateSample.hpp
template<typename T>
class TemplateSample
{
public:
enum CodePath
{
Path1 ,
Path2 ,
Path3 ,
};
TemplateSample(const T& value)
: data(value)
{
}
int doSomething(CodePath path)
{
switch(path)
{
case Path1:
return 1;
case Path2:
return 2;
case Path3:
return 3;
default:
return 0;
}
return -1;
}
template<typename U>
U& returnRefParam(U& refParam)
{
instantiatedCode();
return refParam;
}
template<typename U, typename R>
R doSomethingElse(const U& param)
{
return static_cast<R>(data);
}
private:
void instantiatedCode()
{
int x = 5;
x = x * 10;
}
void neverInstantiatedCode()
{
int x = 5;
x = x * 10;
}
T data;
};
TemplateSampleTest.cpp
#include <string>
#include "gtest/gtest.h"
#include "TemplateSample.hpp"
class TemplateSampleTest : 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);
}
ここでlcovから生成されたコードカバレッジ出力を参照してください。
TemplateSample.hppカバレッジ
警告:「関数」の統計は100%として報告されますが、インスタンス化されていないテンプレート関数に関しては実際には当てはまりません。