ubuntu 16.04 に cpputest, lcov は apt-get で入れた環境で
以下のソースにてテストコードを作成しましたが、
どうしても Branches カバレッジ 100% にできません。
処理のルートはすべてカバーしているつもりですが
何か抜けがあるのでしょうか?
またはカバレッジ作成のオプションが悪いのでしょうか?
対象ソース:
C
1#include <stdio.h> 2#include <sys/stat.h> 3int check_param(const int p1, const int p2, const int p3, const char *fname) 4{ 5 struct stat st; 6 if( p1 == 0){ 7 return 0; 8 } else if( p2 == 0){ 9 return 0; 10 } else if( p3 == 0){ 11 return 0; 12 }else{ 13 if(stat(fname, &st)==0){ 14 printf("exist %s\n", fname); 15 } else { 16 printf("not exist %s\n", fname); 17 } 18 return 1; 19 } 20} 21 22void Open_Data(const int p1, const int p2, const int p3, const char *fname) 23{ 24 if(0 == check_param(p1,p2,p3, fname)){ 25 printf("param error(%d,%d,%d)\n",p1,p2,p3); 26 } else { 27 FILE *fp = fopen(fname, "at"); 28 if(NULL == fp){ 29 printf("can't open %s\n", fname); 30 } else { 31 fclose(fp); 32 } 33 } 34}
テストソース:
C
1#include <CppUTest/CommandLineTestRunner.h> 2#include "target.c" 3 4TEST_GROUP(addG) { 5 void setup() { } 6 void teardown() { } 7}; 8 9TEST(addG, addTest) { 10 system("rm -f /tmp/data1.txt"); 11 12 printf("start Open_DataTest\n"); 13 Open_Data(0,1,1,"/tmp/data1.txt"); 14 Open_Data(1,0,1,"/tmp/data1.txt"); 15 Open_Data(1,1,0,"/tmp/data1.txt"); 16 Open_Data(1,1,1,"/tmp/data1.txt"); // not exist file 17 Open_Data(1,1,1,"/tmp/data1.txt"); // exist file 18 Open_Data(1,1,1,"/tmp/not_exist_dir/data1.txt"); 19 printf("end of test\n"); 20} 21 22int main(int argc, char *argv[]) { 23 return CommandLineTestRunner::RunAllTests(argc, argv); 24}
カバレッジ結果は以下の様になります(赤いマイナス記号が消えません):
手順は以下の様になります
コンパイル:
g++ -fprofile-arcs -ftest-coverage -I./ -o test test.cpp -lCppUTest -lCppUTestExt -coverage
実行:
./test
カバレッジデータ作成:
lcov -c -d . -o test.info --rc lcov_branch_coverage=1
genhtml -o out --branch-coverage test.info
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/21 13:02