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?

gexicide
  • 38,535
  • 21
  • 92
  • 152
  • Depending how you name your tests, you can use [filters](https://code.google.com/p/googletest/wiki/V1_6_AdvancedGuide#Running_a_Subset_of_the_Tests) to run only a subset of your tests. – Justin Wood Sep 23 '14 at 15:41
  • 2
    @JustinWood: I know. But this is a totally different thing. – gexicide Sep 23 '14 at 15:42
  • 1
    how about putting tests with different type requirements/applicability in a different testcase? – Adam Kosiorek Sep 23 '14 at 16:02
  • @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. – gexicide Sep 23 '14 at 16:14
  • 1
    I am having exactly the same issue. Did you come up with a good solution? – gsf Feb 07 '15 at 18:50
  • @gsf: Well, see my answer. That was all I came up with, but it suffices for my use cases. – gexicide Feb 07 '15 at 20:48
  • 1
    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. – gsf Feb 08 '15 at 03:42
  • For what its worth, there is an open issue for this on gtests github. https://github.com/google/googletest/issues/490. Feel free to voice your 'reaction' there as well and subscribe for the latest status regarding this. – bgura Nov 30 '17 at 20:51
  • https://github.com/google/googletest/pull/1544 – aprelev Apr 04 '18 at 17:54

2 Answers2

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.

gexicide
  • 38,535
  • 21
  • 92
  • 152
3

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
}
mattdibi
  • 641
  • 10
  • 23
Seriously
  • 884
  • 1
  • 11
  • 25