Add support for named value-parameterized tests.

This commit is contained in:
kosak
2015-07-24 19:46:18 +00:00
parent 41b5b28d48
commit 794ef030eb
6 changed files with 351 additions and 18 deletions

View File

@@ -809,6 +809,157 @@ TEST_P(NamingTest, TestsReportCorrectNamesAndParameters) {
INSTANTIATE_TEST_CASE_P(ZeroToFiveSequence, NamingTest, Range(0, 5));
// Tests that user supplied custom parameter names are working correctly.
// Runs the test with a builtin helper method which uses PrintToString,
// as well as a custom function and custom functor to ensure all possible
// uses work correctly.
class CustomFunctorNamingTest : public TestWithParam<std::string> {};
TEST_P(CustomFunctorNamingTest, CustomTestNames) {}
struct CustomParamNameFunctor {
std::string operator()(const ::testing::TestParamInfo<std::string>& info) {
return info.param;
}
};
INSTANTIATE_TEST_CASE_P(CustomParamNameFunctor,
CustomFunctorNamingTest,
Values(std::string("FunctorName")),
CustomParamNameFunctor());
INSTANTIATE_TEST_CASE_P(AllAllowedCharacters,
CustomFunctorNamingTest,
Values("abcdefghijklmnopqrstuvwxyz",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"01234567890_"),
CustomParamNameFunctor());
inline std::string CustomParamNameFunction(
const ::testing::TestParamInfo<std::string>& info) {
return info.param;
}
class CustomFunctionNamingTest : public TestWithParam<std::string> {};
TEST_P(CustomFunctionNamingTest, CustomTestNames) {}
INSTANTIATE_TEST_CASE_P(CustomParamNameFunction,
CustomFunctionNamingTest,
Values(std::string("FunctionName")),
CustomParamNameFunction);
#if GTEST_LANG_CXX11
// Test custom naming with a lambda
class CustomLambdaNamingTest : public TestWithParam<std::string> {};
TEST_P(CustomLambdaNamingTest, CustomTestNames) {}
INSTANTIATE_TEST_CASE_P(CustomParamNameLambda,
CustomLambdaNamingTest,
Values(std::string("LambdaName")),
[](const ::testing::TestParamInfo<std::string>& info) {
return info.param;
});
#endif // GTEST_LANG_CXX11
TEST(CustomNamingTest, CheckNameRegistry) {
::testing::UnitTest* unit_test = ::testing::UnitTest::GetInstance();
std::set<std::string> test_names;
for (int case_num = 0;
case_num < unit_test->total_test_case_count();
++case_num) {
const ::testing::TestCase* test_case = unit_test->GetTestCase(case_num);
for (int test_num = 0;
test_num < test_case->total_test_count();
++test_num) {
const ::testing::TestInfo* test_info = test_case->GetTestInfo(test_num);
test_names.insert(std::string(test_info->name()));
}
}
EXPECT_EQ(1u, test_names.count("CustomTestNames/FunctorName"));
EXPECT_EQ(1u, test_names.count("CustomTestNames/FunctionName"));
#if GTEST_LANG_CXX11
EXPECT_EQ(1u, test_names.count("CustomTestNames/LambdaName"));
#endif // GTEST_LANG_CXX11
}
// Test a numeric name to ensure PrintToStringParamName works correctly.
class CustomIntegerNamingTest : public TestWithParam<int> {};
TEST_P(CustomIntegerNamingTest, TestsReportCorrectNames) {
const ::testing::TestInfo* const test_info =
::testing::UnitTest::GetInstance()->current_test_info();
Message test_name_stream;
test_name_stream << "TestsReportCorrectNames/" << GetParam();
EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
}
INSTANTIATE_TEST_CASE_P(PrintToString,
CustomIntegerNamingTest,
Range(0, 5),
::testing::PrintToStringParamName());
// Test a custom struct with PrintToString.
struct CustomStruct {
explicit CustomStruct(int value) : x(value) {}
int x;
};
std::ostream& operator<<(std::ostream& stream, const CustomStruct& val) {
stream << val.x;
return stream;
}
class CustomStructNamingTest : public TestWithParam<CustomStruct> {};
TEST_P(CustomStructNamingTest, TestsReportCorrectNames) {
const ::testing::TestInfo* const test_info =
::testing::UnitTest::GetInstance()->current_test_info();
Message test_name_stream;
test_name_stream << "TestsReportCorrectNames/" << GetParam();
EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
}
INSTANTIATE_TEST_CASE_P(PrintToString,
CustomStructNamingTest,
Values(CustomStruct(0), CustomStruct(1)),
::testing::PrintToStringParamName());
// Test that using a stateful parameter naming function works as expected.
struct StatefulNamingFunctor {
StatefulNamingFunctor() : sum(0) {}
std::string operator()(const ::testing::TestParamInfo<int>& info) {
int value = info.param + sum;
sum += info.param;
return ::testing::PrintToString(value);
}
int sum;
};
class StatefulNamingTest : public ::testing::TestWithParam<int> {
protected:
StatefulNamingTest() : sum_(0) {}
int sum_;
};
TEST_P(StatefulNamingTest, TestsReportCorrectNames) {
const ::testing::TestInfo* const test_info =
::testing::UnitTest::GetInstance()->current_test_info();
sum_ += GetParam();
Message test_name_stream;
test_name_stream << "TestsReportCorrectNames/" << sum_;
EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
}
INSTANTIATE_TEST_CASE_P(StatefulNamingFunctor,
StatefulNamingTest,
Range(0, 5),
StatefulNamingFunctor());
// Class that cannot be streamed into an ostream. It needs to be copyable
// (and, in case of MSVC, also assignable) in order to be a test parameter
// type. Its default copy constructor and assignment operator do exactly

View File

@@ -755,6 +755,32 @@ TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
#endif // GTEST_HAS_EXCEPTIONS
// This #ifdef block tests the output of value-parameterized tests.
#if GTEST_HAS_PARAM_TEST
std::string ParamNameFunc(const testing::TestParamInfo<std::string>& info) {
return info.param;
}
class ParamTest : public testing::TestWithParam<std::string> {
};
TEST_P(ParamTest, Success) {
EXPECT_EQ("a", GetParam());
}
TEST_P(ParamTest, Failure) {
EXPECT_EQ("b", GetParam()) << "Expected failure";
}
INSTANTIATE_TEST_CASE_P(PrintingStrings,
ParamTest,
testing::Values(std::string("a")),
ParamNameFunc);
#endif // GTEST_HAS_PARAM_TEST
// This #ifdef block tests the output of typed tests.
#if GTEST_HAS_TYPED_TEST

View File

@@ -7,7 +7,7 @@ Expected: true
gtest_output_test_.cc:#: Failure
Value of: 3
Expected: 2
[==========] Running 64 tests from 28 test cases.
[==========] Running 66 tests from 29 test cases.
[----------] Global test environment set-up.
FooEnvironment::SetUp() called.
BarEnvironment::SetUp() called.
@@ -601,6 +601,16 @@ Value of: GetParam()
Actual: 2
Expected: 1
[ FAILED ] PrintingFailingParams/FailingParamTest.Fails/0, where GetParam() = 2
[----------] 2 tests from PrintingStrings/ParamTest
[ RUN ] PrintingStrings/ParamTest.Success/a
[ OK ] PrintingStrings/ParamTest.Success/a
[ RUN ] PrintingStrings/ParamTest.Failure/a
gtest_output_test_.cc:#: Failure
Value of: GetParam()
Actual: "a"
Expected: "b"
Expected failure
[ FAILED ] PrintingStrings/ParamTest.Failure/a, where GetParam() = "a"
[----------] Global test environment tear-down
BarEnvironment::TearDown() called.
gtest_output_test_.cc:#: Failure
@@ -610,9 +620,9 @@ FooEnvironment::TearDown() called.
gtest_output_test_.cc:#: Failure
Failed
Expected fatal failure.
[==========] 64 tests from 28 test cases ran.
[ PASSED ] 21 tests.
[ FAILED ] 43 tests, listed below:
[==========] 66 tests from 29 test cases ran.
[ PASSED ] 22 tests.
[ FAILED ] 44 tests, listed below:
[ FAILED ] NonfatalFailureTest.EscapesStringOperands
[ FAILED ] NonfatalFailureTest.DiffForLongStrings
[ FAILED ] FatalFailureTest.FatalFailureInSubroutine
@@ -656,8 +666,9 @@ Expected fatal failure.
[ FAILED ] ExpectFailureWithThreadsTest.ExpectNonFatalFailure
[ FAILED ] ScopedFakeTestPartResultReporterTest.InterceptOnlyCurrentThread
[ FAILED ] PrintingFailingParams/FailingParamTest.Fails/0, where GetParam() = 2
[ FAILED ] PrintingStrings/ParamTest.Failure/a, where GetParam() = "a"
43 FAILED TESTS
44 FAILED TESTS
 YOU HAVE 1 DISABLED TEST
Note: Google Test filter = FatalFailureTest.*:LoggingTest.*