userver: utest::PrintTestName Struct Reference
Loading...
Searching...
No Matches
utest::PrintTestName Struct Referencefinal

#include <userver/utest/parameter_names.hpp>

Detailed Description

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.

// Declare a structure to keep input parameters for a test.
struct TestParams {
int id;
std::string test_name;
};
// Define a class for running tests which get parameterized with the structure
// defined above.
class ParametrizedTest : public testing::TestWithParam<TestParams> {};
// Create a list of parameters' values, each item corresponding to a separate
// test run with specific input.
const std::vector<TestParams> kTestParams = {
{1, "First"},
{2, "Second"},
{3, "Third"},
};
TEST_P(ParametrizedTest, BasicTest) {
const auto& param = GetParam();
const utest::PrintTestName test_name_printer;
const testing::TestParamInfo<TestParams> param_info(param, /* index */ 0);
EXPECT_EQ(test_name_printer(param_info), param.test_name);
}
// Pass utest::PrintTestName() as the last argument for
// INSTANTIATE_TEST_SUITE_P macro.
INSTANTIATE_TEST_SUITE_P(/* no prefix */, 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 utest::PrintTestName test_name_printer;
const testing::TestParamInfo<AnotherTestParams> param_info(
param,
/* index */ 0
);
EXPECT_EQ(test_name_printer(param_info), "Custom" + param.name);
}
INSTANTIATE_TEST_SUITE_P(
/* no prefix */,
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.

// Define a class for running tests which get parameterized with the structures
// defined above.
class DoublyParametrizedTest : public testing::TestWithParam<std::tuple<TestParams, AnotherTestParams>> {};
TEST_P(DoublyParametrizedTest, BasicTest) {
const auto& param = GetParam();
const utest::PrintTestName test_name_printer;
const testing::TestParamInfo<std::tuple<TestParams, AnotherTestParams>> param_info(param, /* index */ 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 your test with the list of possible inputs defined earlier.
// Pass utest::PrintTestName() as the last argument for
// INSTANTIATE_TEST_SUITE_P macro.
INSTANTIATE_TEST_SUITE_P(
/* no prefix */,
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.

Public Member Functions

template<typename ParamType >
std::string operator() (const testing::TestParamInfo< ParamType > &info) const
 

Member Function Documentation

◆ operator()()

template<typename ParamType >
std::string utest::PrintTestName::operator() ( const testing::TestParamInfo< ParamType > & info) const
inline

Definition at line 102 of file parameter_names.hpp.


The documentation for this struct was generated from the following file: