Implements printing parameters of failed parameterized tests (issue 71).

This commit is contained in:
vladlosev 2010-05-18 21:13:48 +00:00
parent c828e17175
commit 1097b54dcf
7 changed files with 104 additions and 38 deletions

View File

@ -44,6 +44,7 @@
#include <gtest/internal/gtest-internal.h> #include <gtest/internal/gtest-internal.h>
#include <gtest/internal/gtest-linked_ptr.h> #include <gtest/internal/gtest-linked_ptr.h>
#include <gtest/internal/gtest-port.h> #include <gtest/internal/gtest-port.h>
#include <gtest/gtest-printers.h>
#if GTEST_HAS_PARAM_TEST #if GTEST_HAS_PARAM_TEST
@ -171,7 +172,7 @@ class ParamGenerator {
iterator end() const { return iterator(impl_->End()); } iterator end() const { return iterator(impl_->End()); }
private: private:
::testing::internal::linked_ptr<const ParamGeneratorInterface<T> > impl_; linked_ptr<const ParamGeneratorInterface<T> > impl_;
}; };
// Generates values from a range of two comparable values. Can be used to // Generates values from a range of two comparable values. Can be used to
@ -504,12 +505,12 @@ class ParameterizedTestCaseInfo : public ParameterizedTestCaseInfoBase {
param_it != generator.end(); ++param_it, ++i) { param_it != generator.end(); ++param_it, ++i) {
Message test_name_stream; Message test_name_stream;
test_name_stream << test_info->test_base_name.c_str() << "/" << i; 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_case_name_stream.GetString().c_str(),
test_name_stream.GetString().c_str(), test_name_stream.GetString().c_str(),
"", // test_case_comment "", // test_case_comment
"", // comment; TODO(vladl@google.com): provide parameter value comment.c_str(),
// representation.
GetTestCaseTypeId(), GetTestCaseTypeId(),
TestCase::SetUpTestCase, TestCase::SetUpTestCase,
TestCase::TearDownTestCase, TestCase::TearDownTestCase,

View File

@ -2658,6 +2658,19 @@ void ColoredPrintf(GTestColor color, const char* fmt, ...) {
va_end(args); 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. // This class implements the TestEventListener interface.
// //
// Class PrettyUnitTestResultPrinter is copyable. // Class PrettyUnitTestResultPrinter is copyable.
@ -2748,11 +2761,7 @@ void PrettyUnitTestResultPrinter::OnTestCaseStart(const TestCase& test_case) {
void PrettyUnitTestResultPrinter::OnTestStart(const TestInfo& test_info) { void PrettyUnitTestResultPrinter::OnTestStart(const TestInfo& test_info) {
ColoredPrintf(COLOR_GREEN, "[ RUN ] "); ColoredPrintf(COLOR_GREEN, "[ RUN ] ");
PrintTestName(test_case_name_.c_str(), test_info.name()); PrintTestName(test_case_name_.c_str(), test_info.name());
if (test_info.comment()[0] == '\0') {
printf("\n"); printf("\n");
} else {
printf(", where %s\n", test_info.comment());
}
fflush(stdout); fflush(stdout);
} }
@ -2775,6 +2784,9 @@ void PrettyUnitTestResultPrinter::OnTestEnd(const TestInfo& test_info) {
ColoredPrintf(COLOR_RED, "[ FAILED ] "); ColoredPrintf(COLOR_RED, "[ FAILED ] ");
} }
PrintTestName(test_case_name_.c_str(), test_info.name()); PrintTestName(test_case_name_.c_str(), test_info.name());
if (test_info.result()->Failed())
PrintFullTestCommentIfPresent(test_info);
if (GTEST_FLAG(print_time)) { if (GTEST_FLAG(print_time)) {
printf(" (%s ms)\n", internal::StreamableToString( printf(" (%s ms)\n", internal::StreamableToString(
test_info.result()->elapsed_time()).c_str()); test_info.result()->elapsed_time()).c_str());
@ -2823,15 +2835,8 @@ void PrettyUnitTestResultPrinter::PrintFailedTests(const UnitTest& unit_test) {
} }
ColoredPrintf(COLOR_RED, "[ FAILED ] "); ColoredPrintf(COLOR_RED, "[ FAILED ] ");
printf("%s.%s", test_case.name(), test_info.name()); printf("%s.%s", test_case.name(), test_info.name());
if (test_case.comment()[0] != '\0' || PrintFullTestCommentIfPresent(test_info);
test_info.comment()[0] != '\0') { printf("\n");
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());
} }
} }
} }

View File

@ -792,19 +792,50 @@ INSTANTIATE_TEST_CASE_P(FourElemSequence, SeparateInstanceTest, Range(1, 4));
// sequence element used to instantiate the test. // sequence element used to instantiate the test.
class NamingTest : public TestWithParam<int> {}; class NamingTest : public TestWithParam<int> {};
TEST_P(NamingTest, TestsAreNamedAppropriately) { TEST_P(NamingTest, TestsAreNamedAndCommentedCorrectly) {
const ::testing::TestInfo* const test_info = const ::testing::TestInfo* const test_info =
::testing::UnitTest::GetInstance()->current_test_info(); ::testing::UnitTest::GetInstance()->current_test_info();
EXPECT_STREQ("ZeroToFiveSequence/NamingTest", test_info->test_case_name()); EXPECT_STREQ("ZeroToFiveSequence/NamingTest", test_info->test_case_name());
Message msg; Message index_stream;
msg << "TestsAreNamedAppropriately/" << GetParam(); index_stream << "TestsAreNamedAndCommentedCorrectly/" << GetParam();
EXPECT_STREQ(msg.GetString().c_str(), test_info->name()); 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)); 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<Unstreamable> {};
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 #endif // GTEST_HAS_PARAM_TEST
TEST(CompileTest, CombineIsDefinedOnlyWhenGtestHasParamTestIsDefined) { TEST(CompileTest, CombineIsDefinedOnlyWhenGtestHasParamTestIsDefined) {

View File

@ -240,7 +240,7 @@ SUPPORTS_STACK_TRACES = False
CAN_GENERATE_GOLDEN_FILE = (SUPPORTS_DEATH_TESTS and CAN_GENERATE_GOLDEN_FILE = (SUPPORTS_DEATH_TESTS and
SUPPORTS_TYPED_TESTS and SUPPORTS_TYPED_TESTS and
SUPPORTS_THREADS) (SUPPORTS_THREADS or IS_WINDOWS))
class GTestOutputTest(gtest_test_utils.TestCase): class GTestOutputTest(gtest_test_utils.TestCase):

View File

@ -87,6 +87,20 @@ TEST(PassingTest, PassingTest1) {
TEST(PassingTest, PassingTest2) { TEST(PassingTest, PassingTest2) {
} }
// Tests that parameters of failing parameterized tests are printed in the
// failing test summary.
class FailingParamTest : public testing::TestWithParam<int> {};
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. // Tests catching a fatal failure in a subroutine.
TEST(FatalFailureTest, FatalFailureInSubroutine) { TEST(FatalFailureTest, FatalFailureInSubroutine) {
printf("(expecting a failure that x should be 1)\n"); printf("(expecting a failure that x should be 1)\n");

View File

@ -7,7 +7,7 @@ Expected: true
gtest_output_test_.cc:#: Failure gtest_output_test_.cc:#: Failure
Value of: 3 Value of: 3
Expected: 2 Expected: 2
[==========] Running 60 tests from 25 test cases. [==========] Running 61 tests from 26 test cases.
[----------] Global test environment set-up. [----------] Global test environment set-up.
FooEnvironment::SetUp() called. FooEnvironment::SetUp() called.
BarEnvironment::SetUp() called. BarEnvironment::SetUp() called.
@ -411,7 +411,7 @@ Value of: TypeParam()
Actual: 0 Actual: 0
Expected: 1 Expected: 1
Expected failure Expected failure
[ FAILED ] TypedTest/0.Failure [ FAILED ] TypedTest/0.Failure, where TypeParam = int
[----------] 2 tests from Unsigned/TypedTestP/0, where TypeParam = unsigned char [----------] 2 tests from Unsigned/TypedTestP/0, where TypeParam = unsigned char
[ RUN ] Unsigned/TypedTestP/0.Success [ RUN ] Unsigned/TypedTestP/0.Success
[ OK ] Unsigned/TypedTestP/0.Success [ OK ] Unsigned/TypedTestP/0.Success
@ -422,7 +422,7 @@ Value of: TypeParam()
Expected: 1U Expected: 1U
Which is: 1 Which is: 1
Expected failure 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 [----------] 2 tests from Unsigned/TypedTestP/1, where TypeParam = unsigned int
[ RUN ] Unsigned/TypedTestP/1.Success [ RUN ] Unsigned/TypedTestP/1.Success
[ OK ] Unsigned/TypedTestP/1.Success [ OK ] Unsigned/TypedTestP/1.Success
@ -433,7 +433,7 @@ Value of: TypeParam()
Expected: 1U Expected: 1U
Which is: 1 Which is: 1
Expected failure Expected failure
[ FAILED ] Unsigned/TypedTestP/1.Failure [ FAILED ] Unsigned/TypedTestP/1.Failure, where TypeParam = unsigned int
[----------] 4 tests from ExpectFailureTest [----------] 4 tests from ExpectFailureTest
[ RUN ] ExpectFailureTest.ExpectFatalFailure [ RUN ] ExpectFailureTest.ExpectFatalFailure
(expecting 1 failure) (expecting 1 failure)
@ -564,6 +564,13 @@ gtest_output_test_.cc:#: Failure
Failed Failed
Expected non-fatal failure. Expected non-fatal failure.
[ FAILED ] ScopedFakeTestPartResultReporterTest.InterceptOnlyCurrentThread [ 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 [----------] Global test environment tear-down
BarEnvironment::TearDown() called. BarEnvironment::TearDown() called.
gtest_output_test_.cc:#: Failure gtest_output_test_.cc:#: Failure
@ -573,9 +580,9 @@ FooEnvironment::TearDown() called.
gtest_output_test_.cc:#: Failure gtest_output_test_.cc:#: Failure
Failed Failed
Expected fatal failure. Expected fatal failure.
[==========] 60 tests from 25 test cases ran. [==========] 61 tests from 26 test cases ran.
[ PASSED ] 21 tests. [ PASSED ] 21 tests.
[ FAILED ] 39 tests, listed below: [ FAILED ] 40 tests, listed below:
[ FAILED ] FatalFailureTest.FatalFailureInSubroutine [ FAILED ] FatalFailureTest.FatalFailureInSubroutine
[ FAILED ] FatalFailureTest.FatalFailureInNestedSubroutine [ FAILED ] FatalFailureTest.FatalFailureInNestedSubroutine
[ FAILED ] FatalFailureTest.NonfatalFailureInSubroutine [ FAILED ] FatalFailureTest.NonfatalFailureInSubroutine
@ -615,8 +622,9 @@ Expected fatal failure.
[ FAILED ] ExpectFailureWithThreadsTest.ExpectFatalFailure [ FAILED ] ExpectFailureWithThreadsTest.ExpectFatalFailure
[ FAILED ] ExpectFailureWithThreadsTest.ExpectNonFatalFailure [ FAILED ] ExpectFailureWithThreadsTest.ExpectNonFatalFailure
[ FAILED ] ScopedFakeTestPartResultReporterTest.InterceptOnlyCurrentThread [ FAILED ] ScopedFakeTestPartResultReporterTest.InterceptOnlyCurrentThread
[ FAILED ] PrintingFailingParams/FailingParamTest.Fails/0, where GetParam() = 2
39 FAILED TESTS 40 FAILED TESTS
 YOU HAVE 1 DISABLED TEST  YOU HAVE 1 DISABLED TEST
Note: Google Test filter = FatalFailureTest.*:LoggingTest.* Note: Google Test filter = FatalFailureTest.*:LoggingTest.*

View File

@ -5,7 +5,7 @@ gtest_output_test_.cc:#: error: Value of: false
Expected: true Expected: true
gtest_output_test_.cc:#: error: Value of: 3 gtest_output_test_.cc:#: error: Value of: 3
Expected: 2 Expected: 2
[==========] Running 61 tests from 27 test cases. [==========] Running 62 tests from 28 test cases.
[----------] Global test environment set-up. [----------] Global test environment set-up.
FooEnvironment::SetUp() called. FooEnvironment::SetUp() called.
BarEnvironment::SetUp() called. BarEnvironment::SetUp() called.
@ -369,7 +369,7 @@ gtest_output_test_.cc:#: error: Value of: TypeParam()
Actual: 0 Actual: 0
Expected: 1 Expected: 1
Expected failure Expected failure
[ FAILED ] TypedTest/0.Failure [ FAILED ] TypedTest/0.Failure, where TypeParam = int
[----------] 2 tests from Unsigned/TypedTestP/0, where TypeParam = unsigned char [----------] 2 tests from Unsigned/TypedTestP/0, where TypeParam = unsigned char
[ RUN ] Unsigned/TypedTestP/0.Success [ RUN ] Unsigned/TypedTestP/0.Success
[ OK ] Unsigned/TypedTestP/0.Success [ OK ] Unsigned/TypedTestP/0.Success
@ -379,7 +379,7 @@ gtest_output_test_.cc:#: error: Value of: TypeParam()
Expected: 1U Expected: 1U
Which is: 1 Which is: 1
Expected failure 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 [----------] 2 tests from Unsigned/TypedTestP/1, where TypeParam = unsigned int
[ RUN ] Unsigned/TypedTestP/1.Success [ RUN ] Unsigned/TypedTestP/1.Success
[ OK ] Unsigned/TypedTestP/1.Success [ OK ] Unsigned/TypedTestP/1.Success
@ -389,7 +389,7 @@ gtest_output_test_.cc:#: error: Value of: TypeParam()
Expected: 1U Expected: 1U
Which is: 1 Which is: 1
Expected failure Expected failure
[ FAILED ] Unsigned/TypedTestP/1.Failure [ FAILED ] Unsigned/TypedTestP/1.Failure, where TypeParam = unsigned int
[----------] 4 tests from ExpectFailureTest [----------] 4 tests from ExpectFailureTest
[ RUN ] ExpectFailureTest.ExpectFatalFailure [ RUN ] ExpectFailureTest.ExpectFatalFailure
(expecting 1 failure) (expecting 1 failure)
@ -479,6 +479,12 @@ Failed
Expected non-fatal failure. Expected non-fatal failure.
[ FAILED ] ExpectFailureTest.ExpectNonFatalFailureOnAllThreads [ 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 [----------] Global test environment tear-down
BarEnvironment::TearDown() called. BarEnvironment::TearDown() called.
gtest_output_test_.cc:#: error: Failed gtest_output_test_.cc:#: error: Failed
@ -486,9 +492,9 @@ Expected non-fatal failure.
FooEnvironment::TearDown() called. FooEnvironment::TearDown() called.
gtest_output_test_.cc:#: error: Failed gtest_output_test_.cc:#: error: Failed
Expected fatal failure. Expected fatal failure.
[==========] 61 tests from 27 test cases ran. [==========] 62 tests from 28 test cases ran.
[ PASSED ] 21 tests. [ PASSED ] 21 tests.
[ FAILED ] 40 tests, listed below: [ FAILED ] 41 tests, listed below:
[ FAILED ] FatalFailureTest.FatalFailureInSubroutine [ FAILED ] FatalFailureTest.FatalFailureInSubroutine
[ FAILED ] FatalFailureTest.FatalFailureInNestedSubroutine [ FAILED ] FatalFailureTest.FatalFailureInNestedSubroutine
[ FAILED ] FatalFailureTest.NonfatalFailureInSubroutine [ FAILED ] FatalFailureTest.NonfatalFailureInSubroutine
@ -529,8 +535,9 @@ Expected fatal failure.
[ FAILED ] ExpectFailureTest.ExpectNonFatalFailure [ FAILED ] ExpectFailureTest.ExpectNonFatalFailure
[ FAILED ] ExpectFailureTest.ExpectFatalFailureOnAllThreads [ FAILED ] ExpectFailureTest.ExpectFatalFailureOnAllThreads
[ FAILED ] ExpectFailureTest.ExpectNonFatalFailureOnAllThreads [ FAILED ] ExpectFailureTest.ExpectNonFatalFailureOnAllThreads
[ FAILED ] PrintingFailingParams/FailingParamTest.Fails/0, where GetParam() = 2
40 FAILED TESTS 41 FAILED TESTS
YOU HAVE 1 DISABLED TEST YOU HAVE 1 DISABLED TEST
Note: Google Test filter = FatalFailureTest.*:LoggingTest.* Note: Google Test filter = FatalFailureTest.*:LoggingTest.*