How to signal to gtest that a test wants to skip itself
Asked Answered
K

2

17

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?

Kingsley answered 23/9, 2014 at 15:30 Comment(9)
Depending how you name your tests, you can use filters to run only a subset of your tests.Dichogamy
@JustinWood: I know. But this is a totally different thing.Kingsley
how about putting tests with different type requirements/applicability in a different testcase?Tillio
@AdamKosiorek: This would be okay. However, the information that a test was skipped is very important for me, as it quickly tells me which features a specific TypeParam does not possess. So having a "skip" written in the test result is a lot more meaningful to me than a test that simply does not appear in the test result.Kingsley
I am having exactly the same issue. Did you come up with a good solution?Diversification
@gsf: Well, see my answer. That was all I came up with, but it suffices for my use cases.Kingsley
I will vote for it, because this is what I do as well, but I actually was hoping for something better. Like being able to skip a test from the SetUp for example.Diversification
For what its worth, there is an open issue for this on gtests github. github.com/google/googletest/issues/490. Feel free to voice your 'reaction' there as well and subscribe for the latest status regarding this.Ophir
github.com/google/googletest/pull/1544Photomap
K
9

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.

Kingsley answered 23/9, 2014 at 16:19 Comment(0)
D
4

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
}
Disembark answered 20/3, 2020 at 22:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.