所有测试都失败了,但线条覆盖率约为100%。为什么?

发布于 2025-02-09 00:32:33 字数 1167 浏览 1 评论 0原文

我有一个函数在测试文件中和一个测试程序文件,如下所示:

          Line data    Source code
   1             : #include<iostream>
   2             : using namespace std;
   3           3 : int GreatestOfThree(int a,int b,int c){
   4           3 :   if((a>b) && (a>c)){    //for a > b and a>c case
   5           1 :     return a;
   6             :    }
   7           2 :    else if(b>c){    //for b>c case 
   8           1 :     return b;
   9             :    }
  10             :   else{
  11           1 :     return c;
  12             :   }
  13             :  return 0;
  14             : }

main.cpp

#include "gtest/gtest.h"
#include "main.cpp"
TEST(GreaterTest,AisGreater){
EXPECT_EQ(1,GreatestOfThree(3,1,2));
};
TEST(GreaterTest,BisGreater){
EXPECT_EQ(1,GreatestOfThree(1,3,2));
};
TEST(GreaterTest,CisGreater){
EXPECT_EQ(2,GreatestOfThree(1,2,3));
};
int main(int argc,char**argv)
{
testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

main_test.cpp

,如您在main.cpp的列表中所看到的那样,尽管所有测试都失败了,但它具有100%的覆盖范围。好的,这是合理的。所有行均已执行。但是,我只想包括传递测试执行的行。我该如何实现?

谢谢。

I have a function-under-test file and a tester file as below:

          Line data    Source code
   1             : #include<iostream>
   2             : using namespace std;
   3           3 : int GreatestOfThree(int a,int b,int c){
   4           3 :   if((a>b) && (a>c)){    //for a > b and a>c case
   5           1 :     return a;
   6             :    }
   7           2 :    else if(b>c){    //for b>c case 
   8           1 :     return b;
   9             :    }
  10             :   else{
  11           1 :     return c;
  12             :   }
  13             :  return 0;
  14             : }

main.cpp

#include "gtest/gtest.h"
#include "main.cpp"
TEST(GreaterTest,AisGreater){
EXPECT_EQ(1,GreatestOfThree(3,1,2));
};
TEST(GreaterTest,BisGreater){
EXPECT_EQ(1,GreatestOfThree(1,3,2));
};
TEST(GreaterTest,CisGreater){
EXPECT_EQ(2,GreatestOfThree(1,2,3));
};
int main(int argc,char**argv)
{
testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

main_test.cpp

As you may see in the listing of main.cpp, it has 100% coverage although all tests fail. OK, it is reasonable. All lines are executed. However, I want to include only the lines executed by the passed tests. How can I achieve this?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文