Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate suffix for test according to parameter in gtest frame work

Tags:

c++

googletest

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 ?

like image 368
BaraBashkaD Avatar asked Dec 23 '22 14:12

BaraBashkaD


1 Answers

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

like image 122
Gunnar Avatar answered Apr 13 '23 00:04

Gunnar