How to signal to gtest that a test wants to skip itself
Since gtest release 1.10.0 the macro GTEST_SKIP()
is available so you can do something like this:
TYPED_TEST_P(TheTest, ATest){
if(TypeParam::isUnsuitedForThisTest()){
GTEST_SKIP(); // this ends the test here so no need for return
}
// ... real test code goes here
}
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.