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 // A unit test for Google Test itself. This verifies that the basic
31 // constructs of Google Test work.
33 // Author: wan@google.com (Zhanyong Wan)
35 #include <gtest/gtest-spi.h>
36 #include <gtest/gtest.h>
38 // Indicates that this translation unit is part of Google Test's
39 // implementation. It must come before gtest-internal-inl.h is
40 // included, or there will be a compiler error. This trick is to
41 // prevent a user from accidentally including gtest-internal-inl.h in
43 #define GTEST_IMPLEMENTATION_ 1
44 #include "src/gtest-internal-inl.h"
45 #undef GTEST_IMPLEMENTATION_
49 #if GTEST_IS_THREADSAFE
50 using testing::ScopedFakeTestPartResultReporter;
51 using testing::TestPartResultArray;
53 using testing::internal::Notification;
54 using testing::internal::ThreadWithParam;
57 namespace posix = ::testing::internal::posix;
58 using testing::internal::String;
59 using testing::internal::scoped_ptr;
61 // Tests catching fatal failures.
63 // A subroutine used by the following test.
68 // This function calls a test subroutine, catches the fatal failure it
69 // generates, and then returns early.
70 void TryTestSubroutine() {
71 // Calls a subrountine that yields a fatal failure.
74 // Catches the fatal failure and aborts the test.
76 // The testing::Test:: prefix is necessary when calling
77 // HasFatalFailure() outside of a TEST, TEST_F, or test fixture.
78 if (testing::Test::HasFatalFailure()) return;
80 // If we get here, something is wrong.
81 FAIL() << "This should never be reached.";
84 TEST(PassingTest, PassingTest1) {
87 TEST(PassingTest, PassingTest2) {
90 // Tests catching a fatal failure in a subroutine.
91 TEST(FatalFailureTest, FatalFailureInSubroutine) {
92 printf("(expecting a failure that x should be 1)\n");
97 // Tests catching a fatal failure in a nested subroutine.
98 TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
99 printf("(expecting a failure that x should be 1)\n");
101 // Calls a subrountine that yields a fatal failure.
104 // Catches the fatal failure and aborts the test.
106 // When calling HasFatalFailure() inside a TEST, TEST_F, or test
107 // fixture, the testing::Test:: prefix is not needed.
108 if (HasFatalFailure()) return;
110 // If we get here, something is wrong.
111 FAIL() << "This should never be reached.";
114 // Tests HasFatalFailure() after a failed EXPECT check.
115 TEST(FatalFailureTest, NonfatalFailureInSubroutine) {
116 printf("(expecting a failure on false)\n");
117 EXPECT_TRUE(false); // Generates a nonfatal failure
118 ASSERT_FALSE(HasFatalFailure()); // This should succeed.
121 // Tests interleaving user logging and Google Test assertions.
122 TEST(LoggingTest, InterleavingLoggingAndAssertions) {
123 static const int a[4] = {
127 printf("(expecting 2 failures on (3) >= (a[i]))\n");
128 for (int i = 0; i < static_cast<int>(sizeof(a)/sizeof(*a)); i++) {
129 printf("i == %d\n", i);
134 // Tests the SCOPED_TRACE macro.
136 // A helper function for testing SCOPED_TRACE.
137 void SubWithoutTrace(int n) {
142 // Another helper function for testing SCOPED_TRACE.
143 void SubWithTrace(int n) {
144 SCOPED_TRACE(testing::Message() << "n = " << n);
149 // Tests that SCOPED_TRACE() obeys lexical scopes.
150 TEST(SCOPED_TRACETest, ObeysScopes) {
151 printf("(expected to fail)\n");
153 // There should be no trace before SCOPED_TRACE() is invoked.
154 ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
157 SCOPED_TRACE("Expected trace");
158 // After SCOPED_TRACE(), a failure in the current scope should contain
160 ADD_FAILURE() << "This failure is expected, and should have a trace.";
163 // Once the control leaves the scope of the SCOPED_TRACE(), there
164 // should be no trace again.
165 ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
168 // Tests that SCOPED_TRACE works inside a loop.
169 TEST(SCOPED_TRACETest, WorksInLoop) {
170 printf("(expected to fail)\n");
172 for (int i = 1; i <= 2; i++) {
173 SCOPED_TRACE(testing::Message() << "i = " << i);
179 // Tests that SCOPED_TRACE works in a subroutine.
180 TEST(SCOPED_TRACETest, WorksInSubroutine) {
181 printf("(expected to fail)\n");
187 // Tests that SCOPED_TRACE can be nested.
188 TEST(SCOPED_TRACETest, CanBeNested) {
189 printf("(expected to fail)\n");
191 SCOPED_TRACE(""); // A trace without a message.
196 // Tests that multiple SCOPED_TRACEs can be used in the same scope.
197 TEST(SCOPED_TRACETest, CanBeRepeated) {
198 printf("(expected to fail)\n");
202 << "This failure is expected, and should contain trace point A.";
206 << "This failure is expected, and should contain trace point A and B.";
210 ADD_FAILURE() << "This failure is expected, and should contain "
211 << "trace point A, B, and C.";
215 ADD_FAILURE() << "This failure is expected, and should contain "
216 << "trace point A, B, and D.";
219 #if GTEST_IS_THREADSAFE
220 // Tests that SCOPED_TRACE()s can be used concurrently from multiple
221 // threads. Namely, an assertion should be affected by
222 // SCOPED_TRACE()s in its own thread only.
224 // Here's the sequence of actions that happen in the test:
226 // Thread A (main) | Thread B (spawned)
227 // ===============================|================================
229 // -------------------------------+--------------------------------
230 // waits for n1 | SCOPED_TRACE("Trace B");
231 // | generates failure #1
233 // -------------------------------+--------------------------------
234 // SCOPED_TRACE("Trace A"); | waits for n2
235 // generates failure #2 |
237 // -------------------------------|--------------------------------
238 // waits for n3 | generates failure #3
240 // | generates failure #4
242 // -------------------------------|--------------------------------
243 // generates failure #5 | finishes
245 // generates failure #6 |
246 // -------------------------------|--------------------------------
247 // waits for thread B to finish |
255 static void ThreadWithScopedTrace(CheckPoints* check_points) {
257 SCOPED_TRACE("Trace B");
259 << "Expected failure #1 (in thread B, only trace B alive).";
260 check_points->n1.Notify();
261 check_points->n2.WaitForNotification();
264 << "Expected failure #3 (in thread B, trace A & B both alive).";
265 } // Trace B dies here.
267 << "Expected failure #4 (in thread B, only trace A alive).";
268 check_points->n3.Notify();
271 TEST(SCOPED_TRACETest, WorksConcurrently) {
272 printf("(expecting 6 failures)\n");
274 CheckPoints check_points;
275 ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace,
278 check_points.n1.WaitForNotification();
281 SCOPED_TRACE("Trace A");
283 << "Expected failure #2 (in thread A, trace A & B both alive).";
284 check_points.n2.Notify();
285 check_points.n3.WaitForNotification();
288 << "Expected failure #5 (in thread A, only trace A alive).";
289 } // Trace A dies here.
291 << "Expected failure #6 (in thread A, no trace alive).";
294 #endif // GTEST_IS_THREADSAFE
296 TEST(DisabledTestsWarningTest,
297 DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) {
298 // This test body is intentionally empty. Its sole purpose is for
299 // verifying that the --gtest_also_run_disabled_tests flag
300 // suppresses the "YOU HAVE 12 DISABLED TESTS" warning at the end of
304 // Tests using assertions outside of TEST and TEST_F.
306 // This function creates two failures intentionally.
308 printf("The non-test part of the code is expected to have 2 failures.\n\n");
313 // Runs all TESTs, all TEST_Fs, and the ad hoc test.
316 return RUN_ALL_TESTS();
319 // Tests non-fatal failures in the fixture constructor.
320 class NonFatalFailureInFixtureConstructorTest : public testing::Test {
322 NonFatalFailureInFixtureConstructorTest() {
323 printf("(expecting 5 failures)\n");
324 ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor.";
327 ~NonFatalFailureInFixtureConstructorTest() {
328 ADD_FAILURE() << "Expected failure #5, in the test fixture d'tor.";
331 virtual void SetUp() {
332 ADD_FAILURE() << "Expected failure #2, in SetUp().";
335 virtual void TearDown() {
336 ADD_FAILURE() << "Expected failure #4, in TearDown.";
340 TEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor) {
341 ADD_FAILURE() << "Expected failure #3, in the test body.";
344 // Tests fatal failures in the fixture constructor.
345 class FatalFailureInFixtureConstructorTest : public testing::Test {
347 FatalFailureInFixtureConstructorTest() {
348 printf("(expecting 2 failures)\n");
352 ~FatalFailureInFixtureConstructorTest() {
353 ADD_FAILURE() << "Expected failure #2, in the test fixture d'tor.";
356 virtual void SetUp() {
357 ADD_FAILURE() << "UNEXPECTED failure in SetUp(). "
358 << "We should never get here, as the test fixture c'tor "
359 << "had a fatal failure.";
362 virtual void TearDown() {
363 ADD_FAILURE() << "UNEXPECTED failure in TearDown(). "
364 << "We should never get here, as the test fixture c'tor "
365 << "had a fatal failure.";
369 FAIL() << "Expected failure #1, in the test fixture c'tor.";
373 TEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) {
374 ADD_FAILURE() << "UNEXPECTED failure in the test body. "
375 << "We should never get here, as the test fixture c'tor "
376 << "had a fatal failure.";
379 // Tests non-fatal failures in SetUp().
380 class NonFatalFailureInSetUpTest : public testing::Test {
382 virtual ~NonFatalFailureInSetUpTest() {
386 virtual void SetUp() {
387 printf("(expecting 4 failures)\n");
388 ADD_FAILURE() << "Expected failure #1, in SetUp().";
391 virtual void TearDown() {
392 FAIL() << "Expected failure #3, in TearDown().";
396 FAIL() << "Expected failure #4, in the test fixture d'tor.";
400 TEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) {
401 FAIL() << "Expected failure #2, in the test function.";
404 // Tests fatal failures in SetUp().
405 class FatalFailureInSetUpTest : public testing::Test {
407 virtual ~FatalFailureInSetUpTest() {
411 virtual void SetUp() {
412 printf("(expecting 3 failures)\n");
413 FAIL() << "Expected failure #1, in SetUp().";
416 virtual void TearDown() {
417 FAIL() << "Expected failure #2, in TearDown().";
421 FAIL() << "Expected failure #3, in the test fixture d'tor.";
425 TEST_F(FatalFailureInSetUpTest, FailureInSetUp) {
426 FAIL() << "UNEXPECTED failure in the test function. "
427 << "We should never get here, as SetUp() failed.";
432 // This group of tests verifies that Google Test handles SEH and C++
433 // exceptions correctly.
435 // A function that throws an SEH exception.
436 static void ThrowSEH() {
438 *p = 0; // Raises an access violation.
441 // Tests exceptions thrown in the test fixture constructor.
442 class ExceptionInFixtureCtorTest : public testing::Test {
444 ExceptionInFixtureCtorTest() {
445 printf("(expecting a failure on thrown exception "
446 "in the test fixture's constructor)\n");
451 virtual ~ExceptionInFixtureCtorTest() {
455 virtual void SetUp() {
456 FAIL() << "UNEXPECTED failure in SetUp(). "
457 << "We should never get here, as the test fixture c'tor threw.";
460 virtual void TearDown() {
461 FAIL() << "UNEXPECTED failure in TearDown(). "
462 << "We should never get here, as the test fixture c'tor threw.";
466 FAIL() << "UNEXPECTED failure in the d'tor. "
467 << "We should never get here, as the test fixture c'tor threw.";
471 TEST_F(ExceptionInFixtureCtorTest, ExceptionInFixtureCtor) {
472 FAIL() << "UNEXPECTED failure in the test function. "
473 << "We should never get here, as the test fixture c'tor threw.";
476 // Tests exceptions thrown in SetUp().
477 class ExceptionInSetUpTest : public testing::Test {
479 virtual ~ExceptionInSetUpTest() {
483 virtual void SetUp() {
484 printf("(expecting 3 failures)\n");
489 virtual void TearDown() {
490 FAIL() << "Expected failure #2, in TearDown().";
494 FAIL() << "Expected failure #3, in the test fixture d'tor.";
498 TEST_F(ExceptionInSetUpTest, ExceptionInSetUp) {
499 FAIL() << "UNEXPECTED failure in the test function. "
500 << "We should never get here, as SetUp() threw.";
503 // Tests that TearDown() and the test fixture d'tor are always called,
504 // even when the test function throws an exception.
505 class ExceptionInTestFunctionTest : public testing::Test {
507 virtual ~ExceptionInTestFunctionTest() {
511 virtual void TearDown() {
512 FAIL() << "Expected failure #2, in TearDown().";
516 FAIL() << "Expected failure #3, in the test fixture d'tor.";
520 // Tests that the test fixture d'tor is always called, even when the
521 // test function throws an SEH exception.
522 TEST_F(ExceptionInTestFunctionTest, SEH) {
523 printf("(expecting 3 failures)\n");
528 #if GTEST_HAS_EXCEPTIONS
530 // Tests that the test fixture d'tor is always called, even when the
531 // test function throws a C++ exception. We do this only when
532 // GTEST_HAS_EXCEPTIONS is non-zero, i.e. C++ exceptions are enabled.
533 TEST_F(ExceptionInTestFunctionTest, CppException) {
537 // Tests exceptions thrown in TearDown().
538 class ExceptionInTearDownTest : public testing::Test {
540 virtual ~ExceptionInTearDownTest() {
544 virtual void TearDown() {
549 FAIL() << "Expected failure #2, in the test fixture d'tor.";
553 TEST_F(ExceptionInTearDownTest, ExceptionInTearDown) {
554 printf("(expecting 2 failures)\n");
557 #endif // GTEST_HAS_EXCEPTIONS
559 #endif // GTEST_OS_WINDOWS
561 #if GTEST_IS_THREADSAFE
563 // A unary function that may die.
564 void DieIf(bool should_die) {
565 GTEST_CHECK_(!should_die) << " - death inside DieIf().";
568 // Tests running death tests in a multi-threaded context.
570 // Used for coordination between the main and the spawn thread.
571 struct SpawnThreadNotifications {
572 SpawnThreadNotifications() {}
574 Notification spawn_thread_started;
575 Notification spawn_thread_ok_to_terminate;
578 GTEST_DISALLOW_COPY_AND_ASSIGN_(SpawnThreadNotifications);
581 // The function to be executed in the thread spawn by the
582 // MultipleThreads test (below).
583 static void ThreadRoutine(SpawnThreadNotifications* notifications) {
584 // Signals the main thread that this thread has started.
585 notifications->spawn_thread_started.Notify();
587 // Waits for permission to finish from the main thread.
588 notifications->spawn_thread_ok_to_terminate.WaitForNotification();
591 // This is a death-test test, but it's not named with a DeathTest
592 // suffix. It starts threads which might interfere with later
593 // death tests, so it must run after all other death tests.
594 class DeathTestAndMultiThreadsTest : public testing::Test {
596 // Starts a thread and waits for it to begin.
597 virtual void SetUp() {
598 thread_.reset(new ThreadWithParam<SpawnThreadNotifications*>(
599 &ThreadRoutine, ¬ifications_, NULL));
600 notifications_.spawn_thread_started.WaitForNotification();
602 // Tells the thread to finish, and reaps it.
603 // Depending on the version of the thread library in use,
604 // a manager thread might still be left running that will interfere
605 // with later death tests. This is unfortunate, but this class
606 // cleans up after itself as best it can.
607 virtual void TearDown() {
608 notifications_.spawn_thread_ok_to_terminate.Notify();
612 SpawnThreadNotifications notifications_;
613 scoped_ptr<ThreadWithParam<SpawnThreadNotifications*> > thread_;
616 #endif // GTEST_IS_THREADSAFE
618 // The MixedUpTestCaseTest test case verifies that Google Test will fail a
619 // test if it uses a different fixture class than what other tests in
620 // the same test case use. It deliberately contains two fixture
621 // classes with the same name but defined in different namespaces.
623 // The MixedUpTestCaseWithSameTestNameTest test case verifies that
624 // when the user defines two tests with the same test case name AND
625 // same test name (but in different namespaces), the second test will
630 class MixedUpTestCaseTest : public testing::Test {
633 TEST_F(MixedUpTestCaseTest, FirstTestFromNamespaceFoo) {}
634 TEST_F(MixedUpTestCaseTest, SecondTestFromNamespaceFoo) {}
636 class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
639 TEST_F(MixedUpTestCaseWithSameTestNameTest,
640 TheSecondTestWithThisNameShouldFail) {}
646 class MixedUpTestCaseTest : public testing::Test {
649 // The following two tests are expected to fail. We rely on the
650 // golden file to check that Google Test generates the right error message.
651 TEST_F(MixedUpTestCaseTest, ThisShouldFail) {}
652 TEST_F(MixedUpTestCaseTest, ThisShouldFailToo) {}
654 class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
657 // Expected to fail. We rely on the golden file to check that Google Test
658 // generates the right error message.
659 TEST_F(MixedUpTestCaseWithSameTestNameTest,
660 TheSecondTestWithThisNameShouldFail) {}
664 // The following two test cases verify that Google Test catches the user
665 // error of mixing TEST and TEST_F in the same test case. The first
666 // test case checks the scenario where TEST_F appears before TEST, and
667 // the second one checks where TEST appears before TEST_F.
669 class TEST_F_before_TEST_in_same_test_case : public testing::Test {
672 TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {}
674 // Expected to fail. We rely on the golden file to check that Google Test
675 // generates the right error message.
676 TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {}
678 class TEST_before_TEST_F_in_same_test_case : public testing::Test {
681 TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {}
683 // Expected to fail. We rely on the golden file to check that Google Test
684 // generates the right error message.
685 TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) {
688 // Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE().
689 int global_integer = 0;
691 // Tests that EXPECT_NONFATAL_FAILURE() can reference global variables.
692 TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
694 EXPECT_NONFATAL_FAILURE({
695 EXPECT_EQ(1, global_integer) << "Expected non-fatal failure.";
696 }, "Expected non-fatal failure.");
699 // Tests that EXPECT_NONFATAL_FAILURE() can reference local variables
701 TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
705 EXPECT_NONFATAL_FAILURE({
706 EXPECT_EQ(m, n) << "Expected non-fatal failure.";
707 }, "Expected non-fatal failure.");
710 // Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly
711 // one non-fatal failure and no fatal failure.
712 TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
713 EXPECT_NONFATAL_FAILURE({
714 ADD_FAILURE() << "Expected non-fatal failure.";
715 }, "Expected non-fatal failure.");
718 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is no
719 // non-fatal failure.
720 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
721 printf("(expecting a failure)\n");
722 EXPECT_NONFATAL_FAILURE({
726 // Tests that EXPECT_NONFATAL_FAILURE() fails when there are two
727 // non-fatal failures.
728 TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
729 printf("(expecting a failure)\n");
730 EXPECT_NONFATAL_FAILURE({
731 ADD_FAILURE() << "Expected non-fatal failure 1.";
732 ADD_FAILURE() << "Expected non-fatal failure 2.";
736 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal
738 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
739 printf("(expecting a failure)\n");
740 EXPECT_NONFATAL_FAILURE({
741 FAIL() << "Expected fatal failure.";
745 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
747 TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
748 printf("(expecting a failure)\n");
749 EXPECT_NONFATAL_FAILURE({
754 #if GTEST_HAS_EXCEPTIONS
756 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
758 TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
759 printf("(expecting a failure)\n");
761 EXPECT_NONFATAL_FAILURE({
764 } catch(int) { // NOLINT
768 #endif // GTEST_HAS_EXCEPTIONS
770 // Tests that EXPECT_FATAL_FAILURE() can reference global variables.
771 TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
773 EXPECT_FATAL_FAILURE({
774 ASSERT_EQ(1, global_integer) << "Expected fatal failure.";
775 }, "Expected fatal failure.");
778 // Tests that EXPECT_FATAL_FAILURE() can reference local static
780 TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
783 EXPECT_FATAL_FAILURE({
784 ASSERT_EQ(0, n) << "Expected fatal failure.";
785 }, "Expected fatal failure.");
788 // Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly
789 // one fatal failure and no non-fatal failure.
790 TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
791 EXPECT_FATAL_FAILURE({
792 FAIL() << "Expected fatal failure.";
793 }, "Expected fatal failure.");
796 // Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal
798 TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
799 printf("(expecting a failure)\n");
800 EXPECT_FATAL_FAILURE({
804 // A helper for generating a fatal failure.
805 void FatalFailure() {
806 FAIL() << "Expected fatal failure.";
809 // Tests that EXPECT_FATAL_FAILURE() fails when there are two
811 TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
812 printf("(expecting a failure)\n");
813 EXPECT_FATAL_FAILURE({
819 // Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal
821 TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
822 printf("(expecting a failure)\n");
823 EXPECT_FATAL_FAILURE({
824 ADD_FAILURE() << "Expected non-fatal failure.";
828 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
830 TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
831 printf("(expecting a failure)\n");
832 EXPECT_FATAL_FAILURE({
837 #if GTEST_HAS_EXCEPTIONS
839 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
841 TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
842 printf("(expecting a failure)\n");
844 EXPECT_FATAL_FAILURE({
847 } catch(int) { // NOLINT
851 #endif // GTEST_HAS_EXCEPTIONS
853 // This #ifdef block tests the output of typed tests.
854 #if GTEST_HAS_TYPED_TEST
856 template <typename T>
857 class TypedTest : public testing::Test {
860 TYPED_TEST_CASE(TypedTest, testing::Types<int>);
862 TYPED_TEST(TypedTest, Success) {
863 EXPECT_EQ(0, TypeParam());
866 TYPED_TEST(TypedTest, Failure) {
867 EXPECT_EQ(1, TypeParam()) << "Expected failure";
870 #endif // GTEST_HAS_TYPED_TEST
872 // This #ifdef block tests the output of type-parameterized tests.
873 #if GTEST_HAS_TYPED_TEST_P
875 template <typename T>
876 class TypedTestP : public testing::Test {
879 TYPED_TEST_CASE_P(TypedTestP);
881 TYPED_TEST_P(TypedTestP, Success) {
882 EXPECT_EQ(0U, TypeParam());
885 TYPED_TEST_P(TypedTestP, Failure) {
886 EXPECT_EQ(1U, TypeParam()) << "Expected failure";
889 REGISTER_TYPED_TEST_CASE_P(TypedTestP, Success, Failure);
891 typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
892 INSTANTIATE_TYPED_TEST_CASE_P(Unsigned, TypedTestP, UnsignedTypes);
894 #endif // GTEST_HAS_TYPED_TEST_P
896 #if GTEST_HAS_DEATH_TEST
898 // We rely on the golden file to verify that tests whose test case
899 // name ends with DeathTest are run first.
901 TEST(ADeathTest, ShouldRunFirst) {
904 #if GTEST_HAS_TYPED_TEST
906 // We rely on the golden file to verify that typed tests whose test
907 // case name ends with DeathTest are run first.
909 template <typename T>
910 class ATypedDeathTest : public testing::Test {
913 typedef testing::Types<int, double> NumericTypes;
914 TYPED_TEST_CASE(ATypedDeathTest, NumericTypes);
916 TYPED_TEST(ATypedDeathTest, ShouldRunFirst) {
919 #endif // GTEST_HAS_TYPED_TEST
921 #if GTEST_HAS_TYPED_TEST_P
924 // We rely on the golden file to verify that type-parameterized tests
925 // whose test case name ends with DeathTest are run first.
927 template <typename T>
928 class ATypeParamDeathTest : public testing::Test {
931 TYPED_TEST_CASE_P(ATypeParamDeathTest);
933 TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) {
936 REGISTER_TYPED_TEST_CASE_P(ATypeParamDeathTest, ShouldRunFirst);
938 INSTANTIATE_TYPED_TEST_CASE_P(My, ATypeParamDeathTest, NumericTypes);
940 #endif // GTEST_HAS_TYPED_TEST_P
942 #endif // GTEST_HAS_DEATH_TEST
944 // Tests various failure conditions of
945 // EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}.
946 class ExpectFailureTest : public testing::Test {
947 public: // Must be public and not protected due to a bug in g++ 3.4.2.
952 static void AddFailure(FailureMode failure) {
953 if (failure == FATAL_FAILURE) {
954 FAIL() << "Expected fatal failure.";
956 ADD_FAILURE() << "Expected non-fatal failure.";
961 TEST_F(ExpectFailureTest, ExpectFatalFailure) {
962 // Expected fatal failure, but succeeds.
963 printf("(expecting 1 failure)\n");
964 EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure.");
965 // Expected fatal failure, but got a non-fatal failure.
966 printf("(expecting 1 failure)\n");
967 EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Expected non-fatal "
970 printf("(expecting 1 failure)\n");
971 EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), "Some other fatal failure "
975 TEST_F(ExpectFailureTest, ExpectNonFatalFailure) {
976 // Expected non-fatal failure, but succeeds.
977 printf("(expecting 1 failure)\n");
978 EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure.");
979 // Expected non-fatal failure, but got a fatal failure.
980 printf("(expecting 1 failure)\n");
981 EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure.");
983 printf("(expecting 1 failure)\n");
984 EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Some other non-fatal "
988 #if GTEST_IS_THREADSAFE
990 class ExpectFailureWithThreadsTest : public ExpectFailureTest {
992 static void AddFailureInOtherThread(FailureMode failure) {
993 ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL);
998 TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
999 // We only intercept the current thread.
1000 printf("(expecting 2 failures)\n");
1001 EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE),
1002 "Expected fatal failure.");
1005 TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
1006 // We only intercept the current thread.
1007 printf("(expecting 2 failures)\n");
1008 EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE),
1009 "Expected non-fatal failure.");
1012 typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
1014 // Tests that the ScopedFakeTestPartResultReporter only catches failures from
1015 // the current thread if it is instantiated with INTERCEPT_ONLY_CURRENT_THREAD.
1016 TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
1017 printf("(expecting 2 failures)\n");
1018 TestPartResultArray results;
1020 ScopedFakeTestPartResultReporter reporter(
1021 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
1023 AddFailureInOtherThread(FATAL_FAILURE);
1024 AddFailureInOtherThread(NONFATAL_FAILURE);
1026 // The two failures should not have been intercepted.
1027 EXPECT_EQ(0, results.size()) << "This shouldn't fail.";
1030 #endif // GTEST_IS_THREADSAFE
1032 TEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) {
1033 // Expected fatal failure, but succeeds.
1034 printf("(expecting 1 failure)\n");
1035 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure.");
1036 // Expected fatal failure, but got a non-fatal failure.
1037 printf("(expecting 1 failure)\n");
1038 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
1039 "Expected non-fatal failure.");
1041 printf("(expecting 1 failure)\n");
1042 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
1043 "Some other fatal failure expected.");
1046 TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) {
1047 // Expected non-fatal failure, but succeeds.
1048 printf("(expecting 1 failure)\n");
1049 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected non-fatal "
1051 // Expected non-fatal failure, but got a fatal failure.
1052 printf("(expecting 1 failure)\n");
1053 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
1054 "Expected fatal failure.");
1056 printf("(expecting 1 failure)\n");
1057 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
1058 "Some other non-fatal failure.");
1062 // Two test environments for testing testing::AddGlobalTestEnvironment().
1064 class FooEnvironment : public testing::Environment {
1066 virtual void SetUp() {
1067 printf("%s", "FooEnvironment::SetUp() called.\n");
1070 virtual void TearDown() {
1071 printf("%s", "FooEnvironment::TearDown() called.\n");
1072 FAIL() << "Expected fatal failure.";
1076 class BarEnvironment : public testing::Environment {
1078 virtual void SetUp() {
1079 printf("%s", "BarEnvironment::SetUp() called.\n");
1082 virtual void TearDown() {
1083 printf("%s", "BarEnvironment::TearDown() called.\n");
1084 ADD_FAILURE() << "Expected non-fatal failure.";
1088 GTEST_DEFINE_bool_(internal_skip_environment_and_ad_hoc_tests, false,
1089 "This flag causes the program to skip test environment "
1090 "tests and ad hoc tests.");
1092 // The main function.
1094 // The idea is to use Google Test to run all the tests we have defined (some
1095 // of them are intended to fail), and then compare the test results
1096 // with the "golden" file.
1097 int main(int argc, char **argv) {
1098 testing::GTEST_FLAG(print_time) = false;
1100 // We just run the tests, knowing some of them are intended to fail.
1101 // We will use a separate Python script to compare the output of
1102 // this program with the golden file.
1104 // It's hard to test InitGoogleTest() directly, as it has many
1105 // global side effects. The following line serves as a sanity test
1107 testing::InitGoogleTest(&argc, argv);
1109 String(argv[1]) == "--gtest_internal_skip_environment_and_ad_hoc_tests")
1110 GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = true;
1112 #if GTEST_HAS_DEATH_TEST
1113 if (testing::internal::GTEST_FLAG(internal_run_death_test) != "") {
1114 // Skip the usual output capturing if we're running as the child
1115 // process of an threadsafe-style death test.
1116 #if GTEST_OS_WINDOWS
1117 posix::FReopen("nul:", "w", stdout);
1119 posix::FReopen("/dev/null", "w", stdout);
1120 #endif // GTEST_OS_WINDOWS
1121 return RUN_ALL_TESTS();
1123 #endif // GTEST_HAS_DEATH_TEST
1125 if (GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests))
1126 return RUN_ALL_TESTS();
1128 // Registers two global test environments.
1129 // The golden file verifies that they are set up in the order they
1130 // are registered, and torn down in the reverse order.
1131 testing::AddGlobalTestEnvironment(new FooEnvironment);
1132 testing::AddGlobalTestEnvironment(new BarEnvironment);
1134 return RunAllTests();