Test name printer for parameterized tests written in gtest.
Example usage:
Singly-parameterized test.
Special-purpose field test_name
of type std::string
may be used for describing test input in a human-readable form.
struct TestParams {
int id;
std::string test_name;
};
class ParametrizedTest : public testing::TestWithParam<TestParams> {};
const std::vector<TestParams> kTestParams = {
{1, "First"},
{2, "Second"},
{3, "Third"},
};
TEST_P(ParametrizedTest, BasicTest) {
const auto& param = GetParam();
const testing::TestParamInfo<TestParams> param_info(param, 0);
EXPECT_EQ(test_name_printer(param_info), param.test_name);
}
INSTANTIATE_TEST_SUITE_P(, ParametrizedTest, testing::ValuesIn(kTestParams),
utest::PrintTestName());
This should result in printing test_name
field as a test name during test run.
ParametrizedTest.BasicTest/First
ParametrizedTest.BasicTest/Second
ParametrizedTest.BasicTest/Third
Another option to override a test name.
Helper class ::utest::PrintTestName()
also supports conventional PrintTo
override. However field test_name
of a parameters' structure has a higher priority for overriding a test name than a PrintTo
function.
struct AnotherTestParams {
int id;
std::string name;
};
void PrintTo(const AnotherTestParams& params, std::ostream* output_stream) {
*output_stream << "Custom" << params.name;
}
class AnotherParametrizedTest : public testing::TestWithParam<AnotherTestParams> {};
const std::vector<AnotherTestParams> kAnotherTestParams = {
{1, "First"},
{2, "Second"},
};
TEST_P(AnotherParametrizedTest, BasicTest) {
const auto& param = GetParam();
const testing::TestParamInfo<AnotherTestParams> param_info(
param,
0
);
EXPECT_EQ(test_name_printer(param_info), "Custom" + param.name);
}
INSTANTIATE_TEST_SUITE_P(
,
AnotherParametrizedTest,
testing::ValuesIn(kAnotherTestParams),
);
Doubly-parametrized test.
In case you have more than one dimension for possible test parameters, you can also use ::utest::PrintTestName()
to combine names for the dimensions of every parameter. You can mix methods for overriding test names for the dimension of every parameter independently.
class DoublyParametrizedTest : public testing::TestWithParam<std::tuple<TestParams, AnotherTestParams>> {};
TEST_P(DoublyParametrizedTest, BasicTest) {
const auto& param = GetParam();
const testing::TestParamInfo<std::tuple<TestParams, AnotherTestParams>> param_info(param, 0);
const auto expected_test_name = std::get<0>(param).test_name + "_Custom" + std::get<1>(param).name;
EXPECT_EQ(test_name_printer(param_info), expected_test_name);
}
INSTANTIATE_TEST_SUITE_P(
,
DoublyParametrizedTest,
testing::Combine(testing::ValuesIn(kTestParams), testing::ValuesIn(kAnotherTestParams)),
);
This should result in printing concatenated test names for all combined test parameters dimensions.
DoublyParametrizedTest.BasicTest/First_CustomFirst
DoublyParametrizedTest.BasicTest/First_CustomSecond
DoublyParametrizedTest.BasicTest/Second_CustomFirst
DoublyParametrizedTest.BasicTest/Second_CustomSecond
DoublyParametrizedTest.BasicTest/Third_CustomFirst
DoublyParametrizedTest.BasicTest/Third_CustomSecond
Definition at line 100 of file parameter_names.hpp.