简体   繁体   English

有没有办法创建包含非字母数字字符的 GoogleTest 参数化测试用例?

[英]Is there a way to create GoogleTest paramaterized test cases that contain non-alphanumeric characters?

I'm performing some validation tests on several XML files, some of which contain hyphens in the name.我正在对几个 XML 文件执行一些验证测试,其中一些文件的名称中包含连字符。 I've created a parameterized test case containing the file names (excluding extensions) but GoogleTest fails because我创建了一个包含文件名(不包括扩展名)的参数化测试用例,但 GoogleTest 失败,因为

Note: test names must be non-empty, unique, and may only contain ASCII alphanumeric characters or underscore.注意:测试名称必须是非空的、唯一的,并且只能包含 ASCII 字母数字字符或下划线。 Because PrintToString adds quotes to std::string and C strings, it won't work for these types.因为 PrintToString 将引号添加到 std::string 和 C 字符串,所以它不适用于这些类型。

class ValidateTemplates :public testing::TestWithParam<string>
{
public:
  struct PrintToStringParamName
  {
    template <class ParamType>
    string operator() (const testing::TestParamInfo<ParamType>& info) const
    {
      auto file_name = static_cast<string>(info.param);
      // Remove the file extension because googletest's PrintToString may only
      // contain ASCII alphanumeric characters or underscores
      size_t last_index = file_name.find_last_of(".");
      return file_name.substr(0, last_index);
    }
  };
};

INSTANTIATE_TEST_CASE_P(
  ValidateTemplates,
  ValidateTemplates,
  testing::ValuesIn(list_of_files),
  ValidateTemplates::PrintToStringParamName());

I had the idea of printing the filename with non-alphanumeric characters swapped out for underscores in PrintToStringParamName.我的想法是在 PrintToStringParamName 中用非字母数字字符替换下划线来打印文件名。 But I'd rather keep the parameterized names the same as the file names if possible.但如果可能的话,我宁愿保持参数化名称与文件名相同。

Is there a way to get around this limitation somehow?有没有办法以某种方式绕过这个限制? I can't permanently change the file names and I can't use another testing framework.我无法永久更改文件名,也无法使用其他测试框架。

That is not possible.这是不可能的。 You have already quoted the relevant comment from the documentation.您已经引用了文档中的相关评论。 The reason is that Google Test uses the test name to generate C++ identifiers (class names).原因是 Google Test 使用测试名称生成 C++ 标识符(类名)。 C++ identifiers are limited to alphanumeric characters (and underscores but you should not use underscores in test names ). C++ 标识符仅限于字母数字字符(和下划线,但您不应在测试名称中使用下划线)。

The closest you can get is change the implementation of PrintToStringParamName::operator()() and remove non-alphanumeric characters from the filename.您可以获得的最接近的是更改PrintToStringParamName::operator()()的实现并从文件名中删除非字母数字字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM