根据gtest框架中的参数为test生成后缀

Bar*_*kaD 4 c++ googletest

根据元组参数使用 INSANTIATE_TEST_CASE_P 实例化测试时每个测试都有后缀,默认情况下从零开始运行编号

我怎么能强制有sense suffix?如何覆盖内置参数化测试名称生成器 ::testing::PrintToStringParamName ?

Gun*_*nar 9

我的回答是一个更完整的例子,基于 BaraBashkaD 的回答。

    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() );
Run Code Online (Sandbox Code Playgroud)

您可以从结构中返回 std::string,但不能返回局部变量。

单元测试运行器的结果:

结果在单元测试运行器中