1

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.

jacky
  • 11
  • 1
  • 7
    Coverage answers 'has this line been executed' which is different from 'has this line been executed AND passed tests' which you imply. But that's not the contract. – Dirk Eddelbuettel Jun 20 '22 at 00:40
  • You might be able to achieve what you want, but it would depend on how you’re generating your coverage. – Taekahn Jun 20 '22 at 02:23
  • Re-run only test which success (gtest has parameter to launch specific test). – Jarod42 Jun 20 '22 at 08:59
  • For all the tests that fail, stub them out, run the test suite of the passing tests, view the test coverage of the passing tests. – Eljay Jun 20 '22 at 11:35
  • @DirkEddelbuettel I know but I want "'has this line been executed AND passed tests" :) Isn't there any compact/easy solution for this? Thanks. – jacky Jun 20 '22 at 13:12

0 Answers0