Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to signal to gtest that a test wants to skip itself

Tags:

c++

googletest

I have a set of typed test cases in google test. However, some of these test cases are simply not applicable for a specific type parameter. Consider this example typed test case:

TYPED_TEST_P(TheTest, ATest){
    if(TypeParam::isUnsuitedForThisTest()){
        return;
    }
    // ... real test code goes here
}

This works well, the test is simply skipped. However, when execution the tests, I see a usual

[ RUN      ] XYZ/TheTest/0.ATest
[       OK ] XYZ/TheTest/0.ATest (0 ms) 

so it is not apparent that the test was skipped, it looks like it simply succeeded. I want to somehow display that the test case was skipped. Is there some kind of method in google test to signal that a test case was skipped. Something like this (this does not exist):

TYPED_TEST_P(TheTest, ATest){
    if(TypeParam::isUnsuitedForThisTest()){
        SIGNAL_SKIPPED(); // This is what I would like to have
        return;
    }
    // ... real test code goes here
}

Then, the output would change to something like this:

[ RUN      ] XYZ/TheTest/0.ATest
[  SKIPPED ] XYZ/TheTest/0.ATest (0 ms)

Is there a feature in gtest that enables such a behaviour?

like image 528
gexicide Avatar asked Sep 23 '14 15:09

gexicide


1 Answers

I came up with a simple yet acceptable solution:

Simply print an additional skip line myself using a macro:

#define CHECK_FEATURE_OR_SKIP(FEATURE_NAME) \
do{\
  if(!TypeParam::hasFeature(FEATURE_NAME)) {\
     std::cout << "[  SKIPPED ] Feature " << #FEATURE_NAME << "not supported" << std::endl;\
     return;\
  }\
} while(0)

Then I can simply use this macro:

TYPED_TEST_P(TheTest, ATest){
    CHECK_FEATURE_OR_SKIP(MyFeatureXY);
    // ... real test code goes here
}

The result will look as follows:

[ RUN      ] XYZ/TheTest/0.ATest
[  SKIPPED ] Feature MyFeatureXY not supported 
[       OK ] XYZ/TheTest/0.ATest (0 ms)

The only small flaw is that there is still an OK line, but at least it is apparent that the test case was skipped and also the missing feature is displayed neatly. Another flaw is that a GUI test runner will not display the skip that neatly, but I don't care about this as I only use command line tools to run the test cases.

like image 60
gexicide Avatar answered Sep 19 '22 18:09

gexicide