From 1097b54dcf1cd393e64ec0adf54301a575bbbc1c Mon Sep 17 00:00:00 2001 From: vladlosev Date: Tue, 18 May 2010 21:13:48 +0000 Subject: Implements printing parameters of failed parameterized tests (issue 71). --- include/gtest/internal/gtest-param-util.h | 11 +++++---- src/gtest.cc | 33 +++++++++++++++----------- test/gtest-param-test_test.cc | 39 +++++++++++++++++++++++++++---- test/gtest_output_test.py | 2 +- test/gtest_output_test_.cc | 14 +++++++++++ test/gtest_output_test_golden_lin.txt | 22 +++++++++++------ test/gtest_output_test_golden_win.txt | 21 +++++++++++------ 7 files changed, 104 insertions(+), 38 deletions(-) diff --git a/include/gtest/internal/gtest-param-util.h b/include/gtest/internal/gtest-param-util.h index 0cbb58c..98bcca7 100644 --- a/include/gtest/internal/gtest-param-util.h +++ b/include/gtest/internal/gtest-param-util.h @@ -44,6 +44,7 @@ #include #include #include +#include #if GTEST_HAS_PARAM_TEST @@ -171,7 +172,7 @@ class ParamGenerator { iterator end() const { return iterator(impl_->End()); } private: - ::testing::internal::linked_ptr > impl_; + linked_ptr > impl_; }; // Generates values from a range of two comparable values. Can be used to @@ -285,7 +286,7 @@ class ValuesInIteratorRangeGenerator : public ParamGeneratorInterface { public: Iterator(const ParamGeneratorInterface* base, typename ContainerType::const_iterator iterator) - : base_(base), iterator_(iterator) {} + : base_(base), iterator_(iterator) {} virtual ~Iterator() {} virtual const ParamGeneratorInterface* BaseGenerator() const { @@ -504,12 +505,12 @@ class ParameterizedTestCaseInfo : public ParameterizedTestCaseInfoBase { param_it != generator.end(); ++param_it, ++i) { Message test_name_stream; test_name_stream << test_info->test_base_name.c_str() << "/" << i; - ::testing::internal::MakeAndRegisterTestInfo( + std::string comment = "GetParam() = " + PrintToString(*param_it); + MakeAndRegisterTestInfo( test_case_name_stream.GetString().c_str(), test_name_stream.GetString().c_str(), "", // test_case_comment - "", // comment; TODO(vladl@google.com): provide parameter value - // representation. + comment.c_str(), GetTestCaseTypeId(), TestCase::SetUpTestCase, TestCase::TearDownTestCase, diff --git a/src/gtest.cc b/src/gtest.cc index 302c327..e136a18 100644 --- a/src/gtest.cc +++ b/src/gtest.cc @@ -2658,6 +2658,19 @@ void ColoredPrintf(GTestColor color, const char* fmt, ...) { va_end(args); } +void PrintFullTestCommentIfPresent(const TestInfo& test_info) { + const char* const comment = test_info.comment(); + const char* const test_case_comment = test_info.test_case_comment(); + + if (test_case_comment[0] != '\0' || comment[0] != '\0') { + printf(", where %s", test_case_comment); + if (test_case_comment[0] != '\0' && comment[0] != '\0') { + printf(" and "); + } + printf("%s", comment); + } +} + // This class implements the TestEventListener interface. // // Class PrettyUnitTestResultPrinter is copyable. @@ -2748,11 +2761,7 @@ void PrettyUnitTestResultPrinter::OnTestCaseStart(const TestCase& test_case) { void PrettyUnitTestResultPrinter::OnTestStart(const TestInfo& test_info) { ColoredPrintf(COLOR_GREEN, "[ RUN ] "); PrintTestName(test_case_name_.c_str(), test_info.name()); - if (test_info.comment()[0] == '\0') { - printf("\n"); - } else { - printf(", where %s\n", test_info.comment()); - } + printf("\n"); fflush(stdout); } @@ -2775,6 +2784,9 @@ void PrettyUnitTestResultPrinter::OnTestEnd(const TestInfo& test_info) { ColoredPrintf(COLOR_RED, "[ FAILED ] "); } PrintTestName(test_case_name_.c_str(), test_info.name()); + if (test_info.result()->Failed()) + PrintFullTestCommentIfPresent(test_info); + if (GTEST_FLAG(print_time)) { printf(" (%s ms)\n", internal::StreamableToString( test_info.result()->elapsed_time()).c_str()); @@ -2823,15 +2835,8 @@ void PrettyUnitTestResultPrinter::PrintFailedTests(const UnitTest& unit_test) { } ColoredPrintf(COLOR_RED, "[ FAILED ] "); printf("%s.%s", test_case.name(), test_info.name()); - if (test_case.comment()[0] != '\0' || - test_info.comment()[0] != '\0') { - printf(", where %s", test_case.comment()); - if (test_case.comment()[0] != '\0' && - test_info.comment()[0] != '\0') { - printf(" and "); - } - } - printf("%s\n", test_info.comment()); + PrintFullTestCommentIfPresent(test_info); + printf("\n"); } } } diff --git a/test/gtest-param-test_test.cc b/test/gtest-param-test_test.cc index d0a0e73..26acce4 100644 --- a/test/gtest-param-test_test.cc +++ b/test/gtest-param-test_test.cc @@ -792,19 +792,50 @@ INSTANTIATE_TEST_CASE_P(FourElemSequence, SeparateInstanceTest, Range(1, 4)); // sequence element used to instantiate the test. class NamingTest : public TestWithParam {}; -TEST_P(NamingTest, TestsAreNamedAppropriately) { +TEST_P(NamingTest, TestsAreNamedAndCommentedCorrectly) { const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info(); EXPECT_STREQ("ZeroToFiveSequence/NamingTest", test_info->test_case_name()); - Message msg; - msg << "TestsAreNamedAppropriately/" << GetParam(); - EXPECT_STREQ(msg.GetString().c_str(), test_info->name()); + Message index_stream; + index_stream << "TestsAreNamedAndCommentedCorrectly/" << GetParam(); + EXPECT_STREQ(index_stream.GetString().c_str(), test_info->name()); + + const ::std::string comment = + "GetParam() = " + ::testing::PrintToString(GetParam()); + EXPECT_EQ(comment, test_info->comment()); } INSTANTIATE_TEST_CASE_P(ZeroToFiveSequence, NamingTest, Range(0, 5)); +// 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 +// what we need. +class Unstreamable { + public: + explicit Unstreamable(int value) : value_(value) {} + + private: + int value_; +}; + +class CommentTest : public TestWithParam {}; + +TEST_P(CommentTest, TestsWithUnstreamableParamsCommentedCorrectly) { + const ::testing::TestInfo* const test_info = + ::testing::UnitTest::GetInstance()->current_test_info(); + + const ::std::string comment = + "GetParam() = " + ::testing::PrintToString(GetParam()); + EXPECT_EQ(comment, test_info->comment()); +} + +INSTANTIATE_TEST_CASE_P(InstantiationWithComments, + CommentTest, + Values(Unstreamable(1))); + #endif // GTEST_HAS_PARAM_TEST TEST(CompileTest, CombineIsDefinedOnlyWhenGtestHasParamTestIsDefined) { diff --git a/test/gtest_output_test.py b/test/gtest_output_test.py index dca4af0..425d9da 100755 --- a/test/gtest_output_test.py +++ b/test/gtest_output_test.py @@ -240,7 +240,7 @@ SUPPORTS_STACK_TRACES = False CAN_GENERATE_GOLDEN_FILE = (SUPPORTS_DEATH_TESTS and SUPPORTS_TYPED_TESTS and - SUPPORTS_THREADS) + (SUPPORTS_THREADS or IS_WINDOWS)) class GTestOutputTest(gtest_test_utils.TestCase): diff --git a/test/gtest_output_test_.cc b/test/gtest_output_test_.cc index 32fb49a..1ac439c 100644 --- a/test/gtest_output_test_.cc +++ b/test/gtest_output_test_.cc @@ -87,6 +87,20 @@ TEST(PassingTest, PassingTest1) { TEST(PassingTest, PassingTest2) { } +// Tests that parameters of failing parameterized tests are printed in the +// failing test summary. +class FailingParamTest : public testing::TestWithParam {}; + +TEST_P(FailingParamTest, Fails) { + EXPECT_EQ(1, GetParam()); +} + +// This generates a test which will fail. Google Test is expected to print +// its parameter when it outputs the list of all failed tests. +INSTANTIATE_TEST_CASE_P(PrintingFailingParams, + FailingParamTest, + testing::Values(2)); + // Tests catching a fatal failure in a subroutine. TEST(FatalFailureTest, FatalFailureInSubroutine) { printf("(expecting a failure that x should be 1)\n"); diff --git a/test/gtest_output_test_golden_lin.txt b/test/gtest_output_test_golden_lin.txt index ec60437..2f3994a 100644 --- a/test/gtest_output_test_golden_lin.txt +++ b/test/gtest_output_test_golden_lin.txt @@ -7,7 +7,7 @@ Expected: true gtest_output_test_.cc:#: Failure Value of: 3 Expected: 2 -[==========] Running 60 tests from 25 test cases. +[==========] Running 61 tests from 26 test cases. [----------] Global test environment set-up. FooEnvironment::SetUp() called. BarEnvironment::SetUp() called. @@ -411,7 +411,7 @@ Value of: TypeParam() Actual: 0 Expected: 1 Expected failure -[ FAILED ] TypedTest/0.Failure +[ FAILED ] TypedTest/0.Failure, where TypeParam = int [----------] 2 tests from Unsigned/TypedTestP/0, where TypeParam = unsigned char [ RUN ] Unsigned/TypedTestP/0.Success [ OK ] Unsigned/TypedTestP/0.Success @@ -422,7 +422,7 @@ Value of: TypeParam() Expected: 1U Which is: 1 Expected failure -[ FAILED ] Unsigned/TypedTestP/0.Failure +[ FAILED ] Unsigned/TypedTestP/0.Failure, where TypeParam = unsigned char [----------] 2 tests from Unsigned/TypedTestP/1, where TypeParam = unsigned int [ RUN ] Unsigned/TypedTestP/1.Success [ OK ] Unsigned/TypedTestP/1.Success @@ -433,7 +433,7 @@ Value of: TypeParam() Expected: 1U Which is: 1 Expected failure -[ FAILED ] Unsigned/TypedTestP/1.Failure +[ FAILED ] Unsigned/TypedTestP/1.Failure, where TypeParam = unsigned int [----------] 4 tests from ExpectFailureTest [ RUN ] ExpectFailureTest.ExpectFatalFailure (expecting 1 failure) @@ -564,6 +564,13 @@ gtest_output_test_.cc:#: Failure Failed Expected non-fatal failure. [ FAILED ] ScopedFakeTestPartResultReporterTest.InterceptOnlyCurrentThread +[----------] 1 test from PrintingFailingParams/FailingParamTest +[ RUN ] PrintingFailingParams/FailingParamTest.Fails/0 +gtest_output_test_.cc:#: Failure +Value of: GetParam() + Actual: 2 +Expected: 1 +[ FAILED ] PrintingFailingParams/FailingParamTest.Fails/0, where GetParam() = 2 [----------] Global test environment tear-down BarEnvironment::TearDown() called. gtest_output_test_.cc:#: Failure @@ -573,9 +580,9 @@ FooEnvironment::TearDown() called. gtest_output_test_.cc:#: Failure Failed Expected fatal failure. -[==========] 60 tests from 25 test cases ran. +[==========] 61 tests from 26 test cases ran. [ PASSED ] 21 tests. -[ FAILED ] 39 tests, listed below: +[ FAILED ] 40 tests, listed below: [ FAILED ] FatalFailureTest.FatalFailureInSubroutine [ FAILED ] FatalFailureTest.FatalFailureInNestedSubroutine [ FAILED ] FatalFailureTest.NonfatalFailureInSubroutine @@ -615,8 +622,9 @@ Expected fatal failure. [ FAILED ] ExpectFailureWithThreadsTest.ExpectFatalFailure [ FAILED ] ExpectFailureWithThreadsTest.ExpectNonFatalFailure [ FAILED ] ScopedFakeTestPartResultReporterTest.InterceptOnlyCurrentThread +[ FAILED ] PrintingFailingParams/FailingParamTest.Fails/0, where GetParam() = 2 -39 FAILED TESTS +40 FAILED TESTS  YOU HAVE 1 DISABLED TEST Note: Google Test filter = FatalFailureTest.*:LoggingTest.* diff --git a/test/gtest_output_test_golden_win.txt b/test/gtest_output_test_golden_win.txt index 313c3aa..fb69710 100644 --- a/test/gtest_output_test_golden_win.txt +++ b/test/gtest_output_test_golden_win.txt @@ -5,7 +5,7 @@ gtest_output_test_.cc:#: error: Value of: false Expected: true gtest_output_test_.cc:#: error: Value of: 3 Expected: 2 -[==========] Running 61 tests from 27 test cases. +[==========] Running 62 tests from 28 test cases. [----------] Global test environment set-up. FooEnvironment::SetUp() called. BarEnvironment::SetUp() called. @@ -369,7 +369,7 @@ gtest_output_test_.cc:#: error: Value of: TypeParam() Actual: 0 Expected: 1 Expected failure -[ FAILED ] TypedTest/0.Failure +[ FAILED ] TypedTest/0.Failure, where TypeParam = int [----------] 2 tests from Unsigned/TypedTestP/0, where TypeParam = unsigned char [ RUN ] Unsigned/TypedTestP/0.Success [ OK ] Unsigned/TypedTestP/0.Success @@ -379,7 +379,7 @@ gtest_output_test_.cc:#: error: Value of: TypeParam() Expected: 1U Which is: 1 Expected failure -[ FAILED ] Unsigned/TypedTestP/0.Failure +[ FAILED ] Unsigned/TypedTestP/0.Failure, where TypeParam = unsigned char [----------] 2 tests from Unsigned/TypedTestP/1, where TypeParam = unsigned int [ RUN ] Unsigned/TypedTestP/1.Success [ OK ] Unsigned/TypedTestP/1.Success @@ -389,7 +389,7 @@ gtest_output_test_.cc:#: error: Value of: TypeParam() Expected: 1U Which is: 1 Expected failure -[ FAILED ] Unsigned/TypedTestP/1.Failure +[ FAILED ] Unsigned/TypedTestP/1.Failure, where TypeParam = unsigned int [----------] 4 tests from ExpectFailureTest [ RUN ] ExpectFailureTest.ExpectFatalFailure (expecting 1 failure) @@ -479,6 +479,12 @@ Failed Expected non-fatal failure. [ FAILED ] ExpectFailureTest.ExpectNonFatalFailureOnAllThreads +[----------] 1 test from PrintingFailingParams/FailingParamTest +[ RUN ] PrintingFailingParams/FailingParamTest.Fails/0 +gtest_output_test_.cc:#: error: Value of: GetParam() + Actual: 2 +Expected: 1 +[ FAILED ] PrintingFailingParams/FailingParamTest.Fails/0, where GetParam() = 2 [----------] Global test environment tear-down BarEnvironment::TearDown() called. gtest_output_test_.cc:#: error: Failed @@ -486,9 +492,9 @@ Expected non-fatal failure. FooEnvironment::TearDown() called. gtest_output_test_.cc:#: error: Failed Expected fatal failure. -[==========] 61 tests from 27 test cases ran. +[==========] 62 tests from 28 test cases ran. [ PASSED ] 21 tests. -[ FAILED ] 40 tests, listed below: +[ FAILED ] 41 tests, listed below: [ FAILED ] FatalFailureTest.FatalFailureInSubroutine [ FAILED ] FatalFailureTest.FatalFailureInNestedSubroutine [ FAILED ] FatalFailureTest.NonfatalFailureInSubroutine @@ -529,8 +535,9 @@ Expected fatal failure. [ FAILED ] ExpectFailureTest.ExpectNonFatalFailure [ FAILED ] ExpectFailureTest.ExpectFatalFailureOnAllThreads [ FAILED ] ExpectFailureTest.ExpectNonFatalFailureOnAllThreads +[ FAILED ] PrintingFailingParams/FailingParamTest.Fails/0, where GetParam() = 2 -40 FAILED TESTS +41 FAILED TESTS YOU HAVE 1 DISABLED TEST Note: Google Test filter = FatalFailureTest.*:LoggingTest.* -- cgit v0.12