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-linked_ptr.h>
#include <gtest/internal/gtest-port.h>
#include <gtest/gtest-printers.h>
#if GTEST_HAS_PARAM_TEST
@ -171,7 +172,7 @@ class ParamGenerator {
iterator end() const { return iterator(impl_->End()); }
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
@ -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,

View File

@ -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());
}
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");
}
}
}

View File

@ -792,19 +792,50 @@ INSTANTIATE_TEST_CASE_P(FourElemSequence, SeparateInstanceTest, Range(1, 4));
// sequence element used to instantiate the test.
class NamingTest : public TestWithParam<int> {};
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<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
TEST(CompileTest, CombineIsDefinedOnlyWhenGtestHasParamTestIsDefined) {

View File

@ -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):

View File

@ -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<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.
TEST(FatalFailureTest, FatalFailureInSubroutine) {
printf("(expecting a failure that x should be 1)\n");

View File

@ -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.*

View File

@ -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.*