4

When instantiate test using INSTANTIATE_TEST_CASE_P according to tuple params Each test have suffix that by default running number from zero

How I can force to have sense suffix ? How to override builtin parameterized test name generator ::testing::PrintToStringParamName ?

BaraBashkaD
  • 144
  • 3
  • 10
  • Possible duplicate of [Can I give better names to value-parameterized tests in gtest?](https://stackoverflow.com/questions/17964100/can-i-give-better-names-to-value-parameterized-tests-in-gtest) – Russell Gallop Feb 28 '19 at 14:38

3 Answers3

9

My answer is a more complete example, based on BaraBashkaD's answer.

    struct Location
    {
       std::string Name;
       double Latitude;
       double Longitude;
       std::string CountryCode;
       std::string AdminOneCode;
       std::string LocaleID;
    };

    class NorthAmericaParamTest : public testing::TestWithParam<Location>
    {
    public:
       struct PrintToStringParamName
       {
          template <class ParamType>
          std::string operator()( const testing::TestParamInfo<ParamType>& info ) const
          {
             auto location = static_cast<Location>(info.param);
             return location.Name;
          }
       };
    };

    TEST_P( NorthAmericaParamTest, City )
    {
       // Setup

       // Software Unit Under Test

       // Evaluate

       // Evaluate
       EXPECT_TRUE( true );
    }

    INSTANTIATE_TEST_CASE_P( UnitedStates, NorthAmericaParamTest, testing::Values
    (
       Location{ "Alta", -28.5, -42.84, "Alta", "AltaCouloir", "AltaCouloir" },
       Location{ "Snow", -32.5, -42.84, "Snow", "SnowCouloir", "SnowCouloir" },
       Location{ "Vail", -24.5, -42.84, "Bird", "BirdCouloir", "BirdCouloir" }
    ), NorthAmericaParamTest::PrintToStringParamName() );

You can return a std::string from the struct, but not a local variable.

Results in unit test runner:

result in unit test runner

Gunnar
  • 339
  • 2
  • 13
1

Actually answer found in comment of INSTANTIATE_TEST_CASE_P macro

The optional last argument to INSTANTIATE_TEST_CASE_P allows the user to specify a function or functor that generates custom test name suffixes based on the test parameters. The function should accept one argument of type testing::TestParamInfo, and return std::string.

testing::PrintToStringParamName is a builtin test suffix generator that returns the value of testing::PrintToString(GetParam()). It does not work for std::string or C strings.

Note: test names must be non-empty, unique, and may only contain ASCII alphanumeric characters or underscore

and somewhere in test class should be implemented according to actual test parameter list

struct PrintToStringParamName
{
    template <class ParamType>
    std::string operator()(const ::testing::TestParamInfo<ParamType>& info) const
    {
        ::std::stringstream ss;
        ss << std::get<0>(info.param)
           << std::get<1>(info.param)
           << std::get<2>(info.param)
           << std::get<3>(info.param);
        return ss.str();
   }
}

        

and provide it to macro

INSTANTIATE_TEST_CASE_P(<test name>,
                    <test class>,
                    ::testing::Combine(
                       ::testing::Value(1, 5, 3),
                       ::testing::Range(1, 5, 2),
                       ::testing::Values(0, 3)
                       ::testing::Range(0, 3)
                    ),                       
                  <name space>::PrintToStringParamName()
            );
Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
BaraBashkaD
  • 144
  • 3
  • 10
0

This is not direct answer to your question - but an advice that should help to overcome your problem.

Your problem is that you have bunch of numbers in tests report and, e.g. in case of failure, you want to know what is the problem - counting tests (possible tens of them) is not something people like to do.

So - the solution is to use not one INSTANTIATE_TEST_CASE_P - but quite a few of them - as much as it seems reasonable to quick find problems in case of test failure.

Instead of using INSTANTIATE_TEST_CASE_P that way (to simplify - I used char as param type):

INSTANTIATE_TEST_CASE_P(Xxx, 
                        XxxTest,  
                        Values('a',....,'z', 
                               'A', ..., 'Z',
                                '0', ..., '9'));

Use as many "instantiations" as needed:

INSTANTIATE_TEST_CASE_P(SmallLetters, 
                        XxxTest,  
                        Values('a',....,'z'));
INSTANTIATE_TEST_CASE_P(CapitalLetters, 
                        XxxTest,  
                        Values('A',....,'Z'));
INSTANTIATE_TEST_CASE_P(Digits, 
                        XxxTest,  
                        Values('0', .., '9'));

When you get failure in Digits/XxxTest/2 then it is more meaningful than Xxx/XxxTest/45...

PiotrNycz
  • 23,099
  • 7
  • 66
  • 112