I'm performing some validation tests on several XML files, some of which contain hyphens in the name. I've created a parameterized test case containing the file names (excluding extensions) but GoogleTest fails because
Note: test names must be non-empty, unique, and may only contain ASCII alphanumeric characters or underscore. Because PrintToString adds quotes to std::string and C strings, it won't work for these types.
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. 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). C++ identifiers are limited to alphanumeric characters (and underscores but you should not use underscores in test names).
The closest you can get is change the implementation of PrintToStringParamName::operator()()
and remove non-alphanumeric characters from the filename.