1 // Copyright 2005, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // The purpose of this file is to generate Google Test output under
31 // various conditions. The output will then be verified by
32 // gtest_output_test.py to ensure that Google Test generates the
33 // desired messages. Therefore, most tests in this file are MEANT TO
36 // Author: wan@google.com (Zhanyong Wan)
38 #include "gtest/gtest-spi.h"
39 #include "gtest/gtest.h"
40 #include "src/gtest-internal-inl.h"
44 #if GTEST_IS_THREADSAFE
45 using testing::ScopedFakeTestPartResultReporter
;
46 using testing::TestPartResultArray
;
48 using testing::internal::Notification
;
49 using testing::internal::ThreadWithParam
;
52 namespace posix
= ::testing::internal::posix
;
54 // Tests catching fatal failures.
56 // A subroutine used by the following test.
61 // This function calls a test subroutine, catches the fatal failure it
62 // generates, and then returns early.
63 void TryTestSubroutine() {
64 // Calls a subrountine that yields a fatal failure.
67 // Catches the fatal failure and aborts the test.
69 // The testing::Test:: prefix is necessary when calling
70 // HasFatalFailure() outside of a TEST, TEST_F, or test fixture.
71 if (testing::Test::HasFatalFailure()) return;
73 // If we get here, something is wrong.
74 FAIL() << "This should never be reached.";
77 TEST(PassingTest
, PassingTest1
) {
80 TEST(PassingTest
, PassingTest2
) {
83 // Tests that parameters of failing parameterized tests are printed in the
84 // failing test summary.
85 class FailingParamTest
: public testing::TestWithParam
<int> {};
87 TEST_P(FailingParamTest
, Fails
) {
88 EXPECT_EQ(1, GetParam());
91 // This generates a test which will fail. Google Test is expected to print
92 // its parameter when it outputs the list of all failed tests.
93 INSTANTIATE_TEST_CASE_P(PrintingFailingParams
,
97 static const char kGoldenString
[] = "\"Line\0 1\"\nLine 2";
99 TEST(NonfatalFailureTest
, EscapesStringOperands
) {
100 std::string actual
= "actual \"string\"";
101 EXPECT_EQ(kGoldenString
, actual
);
103 const char* golden
= kGoldenString
;
104 EXPECT_EQ(golden
, actual
);
107 TEST(NonfatalFailureTest
, DiffForLongStrings
) {
108 std::string
golden_str(kGoldenString
, sizeof(kGoldenString
) - 1);
109 EXPECT_EQ(golden_str
, "Line 2");
112 // Tests catching a fatal failure in a subroutine.
113 TEST(FatalFailureTest
, FatalFailureInSubroutine
) {
114 printf("(expecting a failure that x should be 1)\n");
119 // Tests catching a fatal failure in a nested subroutine.
120 TEST(FatalFailureTest
, FatalFailureInNestedSubroutine
) {
121 printf("(expecting a failure that x should be 1)\n");
123 // Calls a subrountine that yields a fatal failure.
126 // Catches the fatal failure and aborts the test.
128 // When calling HasFatalFailure() inside a TEST, TEST_F, or test
129 // fixture, the testing::Test:: prefix is not needed.
130 if (HasFatalFailure()) return;
132 // If we get here, something is wrong.
133 FAIL() << "This should never be reached.";
136 // Tests HasFatalFailure() after a failed EXPECT check.
137 TEST(FatalFailureTest
, NonfatalFailureInSubroutine
) {
138 printf("(expecting a failure on false)\n");
139 EXPECT_TRUE(false); // Generates a nonfatal failure
140 ASSERT_FALSE(HasFatalFailure()); // This should succeed.
143 // Tests interleaving user logging and Google Test assertions.
144 TEST(LoggingTest
, InterleavingLoggingAndAssertions
) {
145 static const int a
[4] = {
149 printf("(expecting 2 failures on (3) >= (a[i]))\n");
150 for (int i
= 0; i
< static_cast<int>(sizeof(a
)/sizeof(*a
)); i
++) {
151 printf("i == %d\n", i
);
156 // Tests the SCOPED_TRACE macro.
158 // A helper function for testing SCOPED_TRACE.
159 void SubWithoutTrace(int n
) {
164 // Another helper function for testing SCOPED_TRACE.
165 void SubWithTrace(int n
) {
166 SCOPED_TRACE(testing::Message() << "n = " << n
);
171 TEST(SCOPED_TRACETest
, AcceptedValues
) {
172 SCOPED_TRACE("literal string");
173 SCOPED_TRACE(std::string("std::string"));
174 SCOPED_TRACE(1337); // streamable type
175 const char* null_value
= NULL
;
176 SCOPED_TRACE(null_value
);
178 ADD_FAILURE() << "Just checking that all these values work fine.";
181 // Tests that SCOPED_TRACE() obeys lexical scopes.
182 TEST(SCOPED_TRACETest
, ObeysScopes
) {
183 printf("(expected to fail)\n");
185 // There should be no trace before SCOPED_TRACE() is invoked.
186 ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
189 SCOPED_TRACE("Expected trace");
190 // After SCOPED_TRACE(), a failure in the current scope should contain
192 ADD_FAILURE() << "This failure is expected, and should have a trace.";
195 // Once the control leaves the scope of the SCOPED_TRACE(), there
196 // should be no trace again.
197 ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
200 // Tests that SCOPED_TRACE works inside a loop.
201 TEST(SCOPED_TRACETest
, WorksInLoop
) {
202 printf("(expected to fail)\n");
204 for (int i
= 1; i
<= 2; i
++) {
205 SCOPED_TRACE(testing::Message() << "i = " << i
);
211 // Tests that SCOPED_TRACE works in a subroutine.
212 TEST(SCOPED_TRACETest
, WorksInSubroutine
) {
213 printf("(expected to fail)\n");
219 // Tests that SCOPED_TRACE can be nested.
220 TEST(SCOPED_TRACETest
, CanBeNested
) {
221 printf("(expected to fail)\n");
223 SCOPED_TRACE(""); // A trace without a message.
228 // Tests that multiple SCOPED_TRACEs can be used in the same scope.
229 TEST(SCOPED_TRACETest
, CanBeRepeated
) {
230 printf("(expected to fail)\n");
234 << "This failure is expected, and should contain trace point A.";
238 << "This failure is expected, and should contain trace point A and B.";
242 ADD_FAILURE() << "This failure is expected, and should "
243 << "contain trace point A, B, and C.";
247 ADD_FAILURE() << "This failure is expected, and should "
248 << "contain trace point A, B, and D.";
251 #if GTEST_IS_THREADSAFE
252 // Tests that SCOPED_TRACE()s can be used concurrently from multiple
253 // threads. Namely, an assertion should be affected by
254 // SCOPED_TRACE()s in its own thread only.
256 // Here's the sequence of actions that happen in the test:
258 // Thread A (main) | Thread B (spawned)
259 // ===============================|================================
261 // -------------------------------+--------------------------------
262 // waits for n1 | SCOPED_TRACE("Trace B");
263 // | generates failure #1
265 // -------------------------------+--------------------------------
266 // SCOPED_TRACE("Trace A"); | waits for n2
267 // generates failure #2 |
269 // -------------------------------|--------------------------------
270 // waits for n3 | generates failure #3
272 // | generates failure #4
274 // -------------------------------|--------------------------------
275 // generates failure #5 | finishes
277 // generates failure #6 |
278 // -------------------------------|--------------------------------
279 // waits for thread B to finish |
287 static void ThreadWithScopedTrace(CheckPoints
* check_points
) {
289 SCOPED_TRACE("Trace B");
291 << "Expected failure #1 (in thread B, only trace B alive).";
292 check_points
->n1
.Notify();
293 check_points
->n2
.WaitForNotification();
296 << "Expected failure #3 (in thread B, trace A & B both alive).";
297 } // Trace B dies here.
299 << "Expected failure #4 (in thread B, only trace A alive).";
300 check_points
->n3
.Notify();
303 TEST(SCOPED_TRACETest
, WorksConcurrently
) {
304 printf("(expecting 6 failures)\n");
306 CheckPoints check_points
;
307 ThreadWithParam
<CheckPoints
*> thread(&ThreadWithScopedTrace
,
310 check_points
.n1
.WaitForNotification();
313 SCOPED_TRACE("Trace A");
315 << "Expected failure #2 (in thread A, trace A & B both alive).";
316 check_points
.n2
.Notify();
317 check_points
.n3
.WaitForNotification();
320 << "Expected failure #5 (in thread A, only trace A alive).";
321 } // Trace A dies here.
323 << "Expected failure #6 (in thread A, no trace alive).";
326 #endif // GTEST_IS_THREADSAFE
328 // Tests basic functionality of the ScopedTrace utility (most of its features
329 // are already tested in SCOPED_TRACETest).
330 TEST(ScopedTraceTest
, WithExplicitFileAndLine
) {
331 testing::ScopedTrace
trace("explicit_file.cc", 123, "expected trace message");
332 ADD_FAILURE() << "Check that the trace is attached to a particular location.";
335 TEST(DisabledTestsWarningTest
,
336 DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning
) {
337 // This test body is intentionally empty. Its sole purpose is for
338 // verifying that the --gtest_also_run_disabled_tests flag
339 // suppresses the "YOU HAVE 12 DISABLED TESTS" warning at the end of
343 // Tests using assertions outside of TEST and TEST_F.
345 // This function creates two failures intentionally.
347 printf("The non-test part of the code is expected to have 2 failures.\n\n");
352 // Runs all TESTs, all TEST_Fs, and the ad hoc test.
355 return RUN_ALL_TESTS();
358 // Tests non-fatal failures in the fixture constructor.
359 class NonFatalFailureInFixtureConstructorTest
: public testing::Test
{
361 NonFatalFailureInFixtureConstructorTest() {
362 printf("(expecting 5 failures)\n");
363 ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor.";
366 ~NonFatalFailureInFixtureConstructorTest() {
367 ADD_FAILURE() << "Expected failure #5, in the test fixture d'tor.";
370 virtual void SetUp() {
371 ADD_FAILURE() << "Expected failure #2, in SetUp().";
374 virtual void TearDown() {
375 ADD_FAILURE() << "Expected failure #4, in TearDown.";
379 TEST_F(NonFatalFailureInFixtureConstructorTest
, FailureInConstructor
) {
380 ADD_FAILURE() << "Expected failure #3, in the test body.";
383 // Tests fatal failures in the fixture constructor.
384 class FatalFailureInFixtureConstructorTest
: public testing::Test
{
386 FatalFailureInFixtureConstructorTest() {
387 printf("(expecting 2 failures)\n");
391 ~FatalFailureInFixtureConstructorTest() {
392 ADD_FAILURE() << "Expected failure #2, in the test fixture d'tor.";
395 virtual void SetUp() {
396 ADD_FAILURE() << "UNEXPECTED failure in SetUp(). "
397 << "We should never get here, as the test fixture c'tor "
398 << "had a fatal failure.";
401 virtual void TearDown() {
402 ADD_FAILURE() << "UNEXPECTED failure in TearDown(). "
403 << "We should never get here, as the test fixture c'tor "
404 << "had a fatal failure.";
409 FAIL() << "Expected failure #1, in the test fixture c'tor.";
413 TEST_F(FatalFailureInFixtureConstructorTest
, FailureInConstructor
) {
414 ADD_FAILURE() << "UNEXPECTED failure in the test body. "
415 << "We should never get here, as the test fixture c'tor "
416 << "had a fatal failure.";
419 // Tests non-fatal failures in SetUp().
420 class NonFatalFailureInSetUpTest
: public testing::Test
{
422 virtual ~NonFatalFailureInSetUpTest() {
426 virtual void SetUp() {
427 printf("(expecting 4 failures)\n");
428 ADD_FAILURE() << "Expected failure #1, in SetUp().";
431 virtual void TearDown() {
432 FAIL() << "Expected failure #3, in TearDown().";
436 FAIL() << "Expected failure #4, in the test fixture d'tor.";
440 TEST_F(NonFatalFailureInSetUpTest
, FailureInSetUp
) {
441 FAIL() << "Expected failure #2, in the test function.";
444 // Tests fatal failures in SetUp().
445 class FatalFailureInSetUpTest
: public testing::Test
{
447 virtual ~FatalFailureInSetUpTest() {
451 virtual void SetUp() {
452 printf("(expecting 3 failures)\n");
453 FAIL() << "Expected failure #1, in SetUp().";
456 virtual void TearDown() {
457 FAIL() << "Expected failure #2, in TearDown().";
461 FAIL() << "Expected failure #3, in the test fixture d'tor.";
465 TEST_F(FatalFailureInSetUpTest
, FailureInSetUp
) {
466 FAIL() << "UNEXPECTED failure in the test function. "
467 << "We should never get here, as SetUp() failed.";
470 TEST(AddFailureAtTest
, MessageContainsSpecifiedFileAndLineNumber
) {
471 ADD_FAILURE_AT("foo.cc", 42) << "Expected failure in foo.cc";
474 #if GTEST_IS_THREADSAFE
476 // A unary function that may die.
477 void DieIf(bool should_die
) {
478 GTEST_CHECK_(!should_die
) << " - death inside DieIf().";
481 // Tests running death tests in a multi-threaded context.
483 // Used for coordination between the main and the spawn thread.
484 struct SpawnThreadNotifications
{
485 SpawnThreadNotifications() {}
487 Notification spawn_thread_started
;
488 Notification spawn_thread_ok_to_terminate
;
491 GTEST_DISALLOW_COPY_AND_ASSIGN_(SpawnThreadNotifications
);
494 // The function to be executed in the thread spawn by the
495 // MultipleThreads test (below).
496 static void ThreadRoutine(SpawnThreadNotifications
* notifications
) {
497 // Signals the main thread that this thread has started.
498 notifications
->spawn_thread_started
.Notify();
500 // Waits for permission to finish from the main thread.
501 notifications
->spawn_thread_ok_to_terminate
.WaitForNotification();
504 // This is a death-test test, but it's not named with a DeathTest
505 // suffix. It starts threads which might interfere with later
506 // death tests, so it must run after all other death tests.
507 class DeathTestAndMultiThreadsTest
: public testing::Test
{
509 // Starts a thread and waits for it to begin.
510 virtual void SetUp() {
511 thread_
.reset(new ThreadWithParam
<SpawnThreadNotifications
*>(
512 &ThreadRoutine
, ¬ifications_
, NULL
));
513 notifications_
.spawn_thread_started
.WaitForNotification();
515 // Tells the thread to finish, and reaps it.
516 // Depending on the version of the thread library in use,
517 // a manager thread might still be left running that will interfere
518 // with later death tests. This is unfortunate, but this class
519 // cleans up after itself as best it can.
520 virtual void TearDown() {
521 notifications_
.spawn_thread_ok_to_terminate
.Notify();
525 SpawnThreadNotifications notifications_
;
526 testing::internal::scoped_ptr
<ThreadWithParam
<SpawnThreadNotifications
*> >
530 #endif // GTEST_IS_THREADSAFE
532 // The MixedUpTestCaseTest test case verifies that Google Test will fail a
533 // test if it uses a different fixture class than what other tests in
534 // the same test case use. It deliberately contains two fixture
535 // classes with the same name but defined in different namespaces.
537 // The MixedUpTestCaseWithSameTestNameTest test case verifies that
538 // when the user defines two tests with the same test case name AND
539 // same test name (but in different namespaces), the second test will
544 class MixedUpTestCaseTest
: public testing::Test
{
547 TEST_F(MixedUpTestCaseTest
, FirstTestFromNamespaceFoo
) {}
548 TEST_F(MixedUpTestCaseTest
, SecondTestFromNamespaceFoo
) {}
550 class MixedUpTestCaseWithSameTestNameTest
: public testing::Test
{
553 TEST_F(MixedUpTestCaseWithSameTestNameTest
,
554 TheSecondTestWithThisNameShouldFail
) {}
560 class MixedUpTestCaseTest
: public testing::Test
{
563 // The following two tests are expected to fail. We rely on the
564 // golden file to check that Google Test generates the right error message.
565 TEST_F(MixedUpTestCaseTest
, ThisShouldFail
) {}
566 TEST_F(MixedUpTestCaseTest
, ThisShouldFailToo
) {}
568 class MixedUpTestCaseWithSameTestNameTest
: public testing::Test
{
571 // Expected to fail. We rely on the golden file to check that Google Test
572 // generates the right error message.
573 TEST_F(MixedUpTestCaseWithSameTestNameTest
,
574 TheSecondTestWithThisNameShouldFail
) {}
578 // The following two test cases verify that Google Test catches the user
579 // error of mixing TEST and TEST_F in the same test case. The first
580 // test case checks the scenario where TEST_F appears before TEST, and
581 // the second one checks where TEST appears before TEST_F.
583 class TEST_F_before_TEST_in_same_test_case
: public testing::Test
{
586 TEST_F(TEST_F_before_TEST_in_same_test_case
, DefinedUsingTEST_F
) {}
588 // Expected to fail. We rely on the golden file to check that Google Test
589 // generates the right error message.
590 TEST(TEST_F_before_TEST_in_same_test_case
, DefinedUsingTESTAndShouldFail
) {}
592 class TEST_before_TEST_F_in_same_test_case
: public testing::Test
{
595 TEST(TEST_before_TEST_F_in_same_test_case
, DefinedUsingTEST
) {}
597 // Expected to fail. We rely on the golden file to check that Google Test
598 // generates the right error message.
599 TEST_F(TEST_before_TEST_F_in_same_test_case
, DefinedUsingTEST_FAndShouldFail
) {
602 // Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE().
603 int global_integer
= 0;
605 // Tests that EXPECT_NONFATAL_FAILURE() can reference global variables.
606 TEST(ExpectNonfatalFailureTest
, CanReferenceGlobalVariables
) {
608 EXPECT_NONFATAL_FAILURE({
609 EXPECT_EQ(1, global_integer
) << "Expected non-fatal failure.";
610 }, "Expected non-fatal failure.");
613 // Tests that EXPECT_NONFATAL_FAILURE() can reference local variables
615 TEST(ExpectNonfatalFailureTest
, CanReferenceLocalVariables
) {
619 EXPECT_NONFATAL_FAILURE({
620 EXPECT_EQ(m
, n
) << "Expected non-fatal failure.";
621 }, "Expected non-fatal failure.");
624 // Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly
625 // one non-fatal failure and no fatal failure.
626 TEST(ExpectNonfatalFailureTest
, SucceedsWhenThereIsOneNonfatalFailure
) {
627 EXPECT_NONFATAL_FAILURE({
628 ADD_FAILURE() << "Expected non-fatal failure.";
629 }, "Expected non-fatal failure.");
632 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is no
633 // non-fatal failure.
634 TEST(ExpectNonfatalFailureTest
, FailsWhenThereIsNoNonfatalFailure
) {
635 printf("(expecting a failure)\n");
636 EXPECT_NONFATAL_FAILURE({
640 // Tests that EXPECT_NONFATAL_FAILURE() fails when there are two
641 // non-fatal failures.
642 TEST(ExpectNonfatalFailureTest
, FailsWhenThereAreTwoNonfatalFailures
) {
643 printf("(expecting a failure)\n");
644 EXPECT_NONFATAL_FAILURE({
645 ADD_FAILURE() << "Expected non-fatal failure 1.";
646 ADD_FAILURE() << "Expected non-fatal failure 2.";
650 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal
652 TEST(ExpectNonfatalFailureTest
, FailsWhenThereIsOneFatalFailure
) {
653 printf("(expecting a failure)\n");
654 EXPECT_NONFATAL_FAILURE({
655 FAIL() << "Expected fatal failure.";
659 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
661 TEST(ExpectNonfatalFailureTest
, FailsWhenStatementReturns
) {
662 printf("(expecting a failure)\n");
663 EXPECT_NONFATAL_FAILURE({
668 #if GTEST_HAS_EXCEPTIONS
670 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
672 TEST(ExpectNonfatalFailureTest
, FailsWhenStatementThrows
) {
673 printf("(expecting a failure)\n");
675 EXPECT_NONFATAL_FAILURE({
678 } catch(int) { // NOLINT
682 #endif // GTEST_HAS_EXCEPTIONS
684 // Tests that EXPECT_FATAL_FAILURE() can reference global variables.
685 TEST(ExpectFatalFailureTest
, CanReferenceGlobalVariables
) {
687 EXPECT_FATAL_FAILURE({
688 ASSERT_EQ(1, global_integer
) << "Expected fatal failure.";
689 }, "Expected fatal failure.");
692 // Tests that EXPECT_FATAL_FAILURE() can reference local static
694 TEST(ExpectFatalFailureTest
, CanReferenceLocalStaticVariables
) {
697 EXPECT_FATAL_FAILURE({
698 ASSERT_EQ(0, n
) << "Expected fatal failure.";
699 }, "Expected fatal failure.");
702 // Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly
703 // one fatal failure and no non-fatal failure.
704 TEST(ExpectFatalFailureTest
, SucceedsWhenThereIsOneFatalFailure
) {
705 EXPECT_FATAL_FAILURE({
706 FAIL() << "Expected fatal failure.";
707 }, "Expected fatal failure.");
710 // Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal
712 TEST(ExpectFatalFailureTest
, FailsWhenThereIsNoFatalFailure
) {
713 printf("(expecting a failure)\n");
714 EXPECT_FATAL_FAILURE({
718 // A helper for generating a fatal failure.
719 void FatalFailure() {
720 FAIL() << "Expected fatal failure.";
723 // Tests that EXPECT_FATAL_FAILURE() fails when there are two
725 TEST(ExpectFatalFailureTest
, FailsWhenThereAreTwoFatalFailures
) {
726 printf("(expecting a failure)\n");
727 EXPECT_FATAL_FAILURE({
733 // Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal
735 TEST(ExpectFatalFailureTest
, FailsWhenThereIsOneNonfatalFailure
) {
736 printf("(expecting a failure)\n");
737 EXPECT_FATAL_FAILURE({
738 ADD_FAILURE() << "Expected non-fatal failure.";
742 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
744 TEST(ExpectFatalFailureTest
, FailsWhenStatementReturns
) {
745 printf("(expecting a failure)\n");
746 EXPECT_FATAL_FAILURE({
751 #if GTEST_HAS_EXCEPTIONS
753 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
755 TEST(ExpectFatalFailureTest
, FailsWhenStatementThrows
) {
756 printf("(expecting a failure)\n");
758 EXPECT_FATAL_FAILURE({
761 } catch(int) { // NOLINT
765 #endif // GTEST_HAS_EXCEPTIONS
767 // This #ifdef block tests the output of value-parameterized tests.
769 std::string
ParamNameFunc(const testing::TestParamInfo
<std::string
>& info
) {
773 class ParamTest
: public testing::TestWithParam
<std::string
> {
776 TEST_P(ParamTest
, Success
) {
777 EXPECT_EQ("a", GetParam());
780 TEST_P(ParamTest
, Failure
) {
781 EXPECT_EQ("b", GetParam()) << "Expected failure";
784 INSTANTIATE_TEST_CASE_P(PrintingStrings
,
786 testing::Values(std::string("a")),
789 // This #ifdef block tests the output of typed tests.
790 #if GTEST_HAS_TYPED_TEST
792 template <typename T
>
793 class TypedTest
: public testing::Test
{
796 TYPED_TEST_CASE(TypedTest
, testing::Types
<int>);
798 TYPED_TEST(TypedTest
, Success
) {
799 EXPECT_EQ(0, TypeParam());
802 TYPED_TEST(TypedTest
, Failure
) {
803 EXPECT_EQ(1, TypeParam()) << "Expected failure";
806 #endif // GTEST_HAS_TYPED_TEST
808 // This #ifdef block tests the output of type-parameterized tests.
809 #if GTEST_HAS_TYPED_TEST_P
811 template <typename T
>
812 class TypedTestP
: public testing::Test
{
815 TYPED_TEST_CASE_P(TypedTestP
);
817 TYPED_TEST_P(TypedTestP
, Success
) {
818 EXPECT_EQ(0U, TypeParam());
821 TYPED_TEST_P(TypedTestP
, Failure
) {
822 EXPECT_EQ(1U, TypeParam()) << "Expected failure";
825 REGISTER_TYPED_TEST_CASE_P(TypedTestP
, Success
, Failure
);
827 typedef testing::Types
<unsigned char, unsigned int> UnsignedTypes
;
828 INSTANTIATE_TYPED_TEST_CASE_P(Unsigned
, TypedTestP
, UnsignedTypes
);
830 #endif // GTEST_HAS_TYPED_TEST_P
832 #if GTEST_HAS_DEATH_TEST
834 // We rely on the golden file to verify that tests whose test case
835 // name ends with DeathTest are run first.
837 TEST(ADeathTest
, ShouldRunFirst
) {
840 # if GTEST_HAS_TYPED_TEST
842 // We rely on the golden file to verify that typed tests whose test
843 // case name ends with DeathTest are run first.
845 template <typename T
>
846 class ATypedDeathTest
: public testing::Test
{
849 typedef testing::Types
<int, double> NumericTypes
;
850 TYPED_TEST_CASE(ATypedDeathTest
, NumericTypes
);
852 TYPED_TEST(ATypedDeathTest
, ShouldRunFirst
) {
855 # endif // GTEST_HAS_TYPED_TEST
857 # if GTEST_HAS_TYPED_TEST_P
860 // We rely on the golden file to verify that type-parameterized tests
861 // whose test case name ends with DeathTest are run first.
863 template <typename T
>
864 class ATypeParamDeathTest
: public testing::Test
{
867 TYPED_TEST_CASE_P(ATypeParamDeathTest
);
869 TYPED_TEST_P(ATypeParamDeathTest
, ShouldRunFirst
) {
872 REGISTER_TYPED_TEST_CASE_P(ATypeParamDeathTest
, ShouldRunFirst
);
874 INSTANTIATE_TYPED_TEST_CASE_P(My
, ATypeParamDeathTest
, NumericTypes
);
876 # endif // GTEST_HAS_TYPED_TEST_P
878 #endif // GTEST_HAS_DEATH_TEST
880 // Tests various failure conditions of
881 // EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}.
882 class ExpectFailureTest
: public testing::Test
{
883 public: // Must be public and not protected due to a bug in g++ 3.4.2.
888 static void AddFailure(FailureMode failure
) {
889 if (failure
== FATAL_FAILURE
) {
890 FAIL() << "Expected fatal failure.";
892 ADD_FAILURE() << "Expected non-fatal failure.";
897 TEST_F(ExpectFailureTest
, ExpectFatalFailure
) {
898 // Expected fatal failure, but succeeds.
899 printf("(expecting 1 failure)\n");
900 EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure.");
901 // Expected fatal failure, but got a non-fatal failure.
902 printf("(expecting 1 failure)\n");
903 EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE
), "Expected non-fatal "
906 printf("(expecting 1 failure)\n");
907 EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE
), "Some other fatal failure "
911 TEST_F(ExpectFailureTest
, ExpectNonFatalFailure
) {
912 // Expected non-fatal failure, but succeeds.
913 printf("(expecting 1 failure)\n");
914 EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure.");
915 // Expected non-fatal failure, but got a fatal failure.
916 printf("(expecting 1 failure)\n");
917 EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE
), "Expected fatal failure.");
919 printf("(expecting 1 failure)\n");
920 EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE
), "Some other non-fatal "
924 #if GTEST_IS_THREADSAFE
926 class ExpectFailureWithThreadsTest
: public ExpectFailureTest
{
928 static void AddFailureInOtherThread(FailureMode failure
) {
929 ThreadWithParam
<FailureMode
> thread(&AddFailure
, failure
, NULL
);
934 TEST_F(ExpectFailureWithThreadsTest
, ExpectFatalFailure
) {
935 // We only intercept the current thread.
936 printf("(expecting 2 failures)\n");
937 EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE
),
938 "Expected fatal failure.");
941 TEST_F(ExpectFailureWithThreadsTest
, ExpectNonFatalFailure
) {
942 // We only intercept the current thread.
943 printf("(expecting 2 failures)\n");
944 EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE
),
945 "Expected non-fatal failure.");
948 typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest
;
950 // Tests that the ScopedFakeTestPartResultReporter only catches failures from
951 // the current thread if it is instantiated with INTERCEPT_ONLY_CURRENT_THREAD.
952 TEST_F(ScopedFakeTestPartResultReporterTest
, InterceptOnlyCurrentThread
) {
953 printf("(expecting 2 failures)\n");
954 TestPartResultArray results
;
956 ScopedFakeTestPartResultReporter
reporter(
957 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD
,
959 AddFailureInOtherThread(FATAL_FAILURE
);
960 AddFailureInOtherThread(NONFATAL_FAILURE
);
962 // The two failures should not have been intercepted.
963 EXPECT_EQ(0, results
.size()) << "This shouldn't fail.";
966 #endif // GTEST_IS_THREADSAFE
968 TEST_F(ExpectFailureTest
, ExpectFatalFailureOnAllThreads
) {
969 // Expected fatal failure, but succeeds.
970 printf("(expecting 1 failure)\n");
971 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure.");
972 // Expected fatal failure, but got a non-fatal failure.
973 printf("(expecting 1 failure)\n");
974 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE
),
975 "Expected non-fatal failure.");
977 printf("(expecting 1 failure)\n");
978 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE
),
979 "Some other fatal failure expected.");
982 TEST_F(ExpectFailureTest
, ExpectNonFatalFailureOnAllThreads
) {
983 // Expected non-fatal failure, but succeeds.
984 printf("(expecting 1 failure)\n");
985 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected non-fatal "
987 // Expected non-fatal failure, but got a fatal failure.
988 printf("(expecting 1 failure)\n");
989 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE
),
990 "Expected fatal failure.");
992 printf("(expecting 1 failure)\n");
993 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE
),
994 "Some other non-fatal failure.");
998 // Two test environments for testing testing::AddGlobalTestEnvironment().
1000 class FooEnvironment
: public testing::Environment
{
1002 virtual void SetUp() {
1003 printf("%s", "FooEnvironment::SetUp() called.\n");
1006 virtual void TearDown() {
1007 printf("%s", "FooEnvironment::TearDown() called.\n");
1008 FAIL() << "Expected fatal failure.";
1012 class BarEnvironment
: public testing::Environment
{
1014 virtual void SetUp() {
1015 printf("%s", "BarEnvironment::SetUp() called.\n");
1018 virtual void TearDown() {
1019 printf("%s", "BarEnvironment::TearDown() called.\n");
1020 ADD_FAILURE() << "Expected non-fatal failure.";
1024 // The main function.
1026 // The idea is to use Google Test to run all the tests we have defined (some
1027 // of them are intended to fail), and then compare the test results
1028 // with the "golden" file.
1029 int main(int argc
, char **argv
) {
1030 testing::GTEST_FLAG(print_time
) = false;
1032 // We just run the tests, knowing some of them are intended to fail.
1033 // We will use a separate Python script to compare the output of
1034 // this program with the golden file.
1036 // It's hard to test InitGoogleTest() directly, as it has many
1037 // global side effects. The following line serves as a sanity test
1039 testing::InitGoogleTest(&argc
, argv
);
1040 bool internal_skip_environment_and_ad_hoc_tests
=
1041 std::count(argv
, argv
+ argc
,
1042 std::string("internal_skip_environment_and_ad_hoc_tests")) > 0;
1044 #if GTEST_HAS_DEATH_TEST
1045 if (testing::internal::GTEST_FLAG(internal_run_death_test
) != "") {
1046 // Skip the usual output capturing if we're running as the child
1047 // process of an threadsafe-style death test.
1048 # if GTEST_OS_WINDOWS
1049 posix::FReopen("nul:", "w", stdout
);
1051 posix::FReopen("/dev/null", "w", stdout
);
1052 # endif // GTEST_OS_WINDOWS
1053 return RUN_ALL_TESTS();
1055 #endif // GTEST_HAS_DEATH_TEST
1057 if (internal_skip_environment_and_ad_hoc_tests
)
1058 return RUN_ALL_TESTS();
1060 // Registers two global test environments.
1061 // The golden file verifies that they are set up in the order they
1062 // are registered, and torn down in the reverse order.
1063 testing::AddGlobalTestEnvironment(new FooEnvironment
);
1064 testing::AddGlobalTestEnvironment(new BarEnvironment
);
1066 return RunAllTests();