所有测试都失败了,但线条覆盖率约为100%。为什么?
我有一个函数在测试文件中和一个测试程序文件,如下所示:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论