多くの大きな (1000 行以上) 関数を含む DLL があります。このコードには多くの複雑なロジックが含まれているため、この DLL を動的にロードしてその API を呼び出すテスト ハーネスを作成しました。
テスト ハーネスから、この API 内でヒットしたコードのブランチをテストできる良い方法を知りたいです。これを行うことを考えることができる唯一の方法は次のとおりです。
// Pseudo test code
void doTest()
{
loadDllToBeTested();
dll_api_function_1();
assert( dll_api_function_1_branches.branch1Hit == true );
unloadDllToBeTested();
}
// Real api in the C dll
struct dll_api_function_1_branches
{
bool branch1Hit;
}
dll_api_function_1_branches g_dll_api_function_1_branches;
int private_func1()
{
printf("Doing my private stuff\n");
return 1;
}
void dll_api_function_1(void)
{
if ( private_func1() )
{
printf("doing some stuff\n");
dll_api_function_1_branches.branch1Hit = true; // so the test code can check if we got here :(
}
else
{
printf("doing some other stuff\n");
}
// tons of other logic and branching...
}
これは基本的に、関数内で特定の分岐に到達したときに値が設定される関数ごとの構造体を持っています。この構造体のグローバルにエクスポートされたインスタンスがあり、テスト コードはこれをゼロに初期化し、API の呼び出し後にチェックする必要があります。
また、ここでは Visual Studio を使用しているため、gcov などのツールは使用できません。