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 that parameters of failing parameterized tests are printed in the
91 // failing test summary.
92 class FailingParamTest : public testing::TestWithParam<int> {};
94 TEST_P(FailingParamTest, Fails) {
95 EXPECT_EQ(1, GetParam());
98 // This generates a test which will fail. Google Test is expected to print
99 // its parameter when it outputs the list of all failed tests.
100 INSTANTIATE_TEST_CASE_P(PrintingFailingParams,
104 // Tests catching a fatal failure in a subroutine.
105 TEST(FatalFailureTest, FatalFailureInSubroutine) {
106 printf("(expecting a failure that x should be 1)\n");
111 // Tests catching a fatal failure in a nested subroutine.
112 TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
113 printf("(expecting a failure that x should be 1)\n");
115 // Calls a subrountine that yields a fatal failure.
118 // Catches the fatal failure and aborts the test.
120 // When calling HasFatalFailure() inside a TEST, TEST_F, or test
121 // fixture, the testing::Test:: prefix is not needed.
122 if (HasFatalFailure()) return;
124 // If we get here, something is wrong.
125 FAIL() << "This should never be reached.";
128 // Tests HasFatalFailure() after a failed EXPECT check.
129 TEST(FatalFailureTest, NonfatalFailureInSubroutine) {
130 printf("(expecting a failure on false)\n");
131 EXPECT_TRUE(false); // Generates a nonfatal failure
132 ASSERT_FALSE(HasFatalFailure()); // This should succeed.
135 // Tests interleaving user logging and Google Test assertions.
136 TEST(LoggingTest, InterleavingLoggingAndAssertions) {
137 static const int a[4] = {
141 printf("(expecting 2 failures on (3) >= (a[i]))\n");
142 for (int i = 0; i < static_cast<int>(sizeof(a)/sizeof(*a)); i++) {
143 printf("i == %d\n", i);
148 // Tests the SCOPED_TRACE macro.
150 // A helper function for testing SCOPED_TRACE.
151 void SubWithoutTrace(int n) {
156 // Another helper function for testing SCOPED_TRACE.
157 void SubWithTrace(int n) {
158 SCOPED_TRACE(testing::Message() << "n = " << n);
163 // Tests that SCOPED_TRACE() obeys lexical scopes.
164 TEST(SCOPED_TRACETest, ObeysScopes) {
165 printf("(expected to fail)\n");
167 // There should be no trace before SCOPED_TRACE() is invoked.
168 ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
171 SCOPED_TRACE("Expected trace");
172 // After SCOPED_TRACE(), a failure in the current scope should contain
174 ADD_FAILURE() << "This failure is expected, and should have a trace.";
177 // Once the control leaves the scope of the SCOPED_TRACE(), there
178 // should be no trace again.
179 ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
182 // Tests that SCOPED_TRACE works inside a loop.
183 TEST(SCOPED_TRACETest, WorksInLoop) {
184 printf("(expected to fail)\n");
186 for (int i = 1; i <= 2; i++) {
187 SCOPED_TRACE(testing::Message() << "i = " << i);
193 // Tests that SCOPED_TRACE works in a subroutine.
194 TEST(SCOPED_TRACETest, WorksInSubroutine) {
195 printf("(expected to fail)\n");
201 // Tests that SCOPED_TRACE can be nested.
202 TEST(SCOPED_TRACETest, CanBeNested) {
203 printf("(expected to fail)\n");
205 SCOPED_TRACE(""); // A trace without a message.
210 // Tests that multiple SCOPED_TRACEs can be used in the same scope.
211 TEST(SCOPED_TRACETest, CanBeRepeated) {
212 printf("(expected to fail)\n");
216 << "This failure is expected, and should contain trace point A.";
220 << "This failure is expected, and should contain trace point A and B.";
224 ADD_FAILURE() << "This failure is expected, and should contain "
225 << "trace point A, B, and C.";
229 ADD_FAILURE() << "This failure is expected, and should contain "
230 << "trace point A, B, and D.";
233 #if GTEST_IS_THREADSAFE
234 // Tests that SCOPED_TRACE()s can be used concurrently from multiple
235 // threads. Namely, an assertion should be affected by
236 // SCOPED_TRACE()s in its own thread only.
238 // Here's the sequence of actions that happen in the test:
240 // Thread A (main) | Thread B (spawned)
241 // ===============================|================================
243 // -------------------------------+--------------------------------
244 // waits for n1 | SCOPED_TRACE("Trace B");
245 // | generates failure #1
247 // -------------------------------+--------------------------------
248 // SCOPED_TRACE("Trace A"); | waits for n2
249 // generates failure #2 |
251 // -------------------------------|--------------------------------
252 // waits for n3 | generates failure #3
254 // | generates failure #4
256 // -------------------------------|--------------------------------
257 // generates failure #5 | finishes
259 // generates failure #6 |
260 // -------------------------------|--------------------------------
261 // waits for thread B to finish |
269 static void ThreadWithScopedTrace(CheckPoints* check_points) {
271 SCOPED_TRACE("Trace B");
273 << "Expected failure #1 (in thread B, only trace B alive).";
274 check_points->n1.Notify();
275 check_points->n2.WaitForNotification();
278 << "Expected failure #3 (in thread B, trace A & B both alive).";
279 } // Trace B dies here.
281 << "Expected failure #4 (in thread B, only trace A alive).";
282 check_points->n3.Notify();
285 TEST(SCOPED_TRACETest, WorksConcurrently) {
286 printf("(expecting 6 failures)\n");
288 CheckPoints check_points;
289 ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace,
292 check_points.n1.WaitForNotification();
295 SCOPED_TRACE("Trace A");
297 << "Expected failure #2 (in thread A, trace A & B both alive).";
298 check_points.n2.Notify();
299 check_points.n3.WaitForNotification();
302 << "Expected failure #5 (in thread A, only trace A alive).";
303 } // Trace A dies here.
305 << "Expected failure #6 (in thread A, no trace alive).";
308 #endif // GTEST_IS_THREADSAFE
310 TEST(DisabledTestsWarningTest,
311 DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) {
312 // This test body is intentionally empty. Its sole purpose is for
313 // verifying that the --gtest_also_run_disabled_tests flag
314 // suppresses the "YOU HAVE 12 DISABLED TESTS" warning at the end of
318 // Tests using assertions outside of TEST and TEST_F.
320 // This function creates two failures intentionally.
322 printf("The non-test part of the code is expected to have 2 failures.\n\n");
327 // Runs all TESTs, all TEST_Fs, and the ad hoc test.
330 return RUN_ALL_TESTS();
333 // Tests non-fatal failures in the fixture constructor.
334 class NonFatalFailureInFixtureConstructorTest : public testing::Test {
336 NonFatalFailureInFixtureConstructorTest() {
337 printf("(expecting 5 failures)\n");
338 ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor.";
341 ~NonFatalFailureInFixtureConstructorTest() {
342 ADD_FAILURE() << "Expected failure #5, in the test fixture d'tor.";
345 virtual void SetUp() {
346 ADD_FAILURE() << "Expected failure #2, in SetUp().";
349 virtual void TearDown() {
350 ADD_FAILURE() << "Expected failure #4, in TearDown.";
354 TEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor) {
355 ADD_FAILURE() << "Expected failure #3, in the test body.";
358 // Tests fatal failures in the fixture constructor.
359 class FatalFailureInFixtureConstructorTest : public testing::Test {
361 FatalFailureInFixtureConstructorTest() {
362 printf("(expecting 2 failures)\n");
366 ~FatalFailureInFixtureConstructorTest() {
367 ADD_FAILURE() << "Expected failure #2, in the test fixture d'tor.";
370 virtual void SetUp() {
371 ADD_FAILURE() << "UNEXPECTED failure in SetUp(). "
372 << "We should never get here, as the test fixture c'tor "
373 << "had a fatal failure.";
376 virtual void TearDown() {
377 ADD_FAILURE() << "UNEXPECTED failure in TearDown(). "
378 << "We should never get here, as the test fixture c'tor "
379 << "had a fatal failure.";
383 FAIL() << "Expected failure #1, in the test fixture c'tor.";
387 TEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) {
388 ADD_FAILURE() << "UNEXPECTED failure in the test body. "
389 << "We should never get here, as the test fixture c'tor "
390 << "had a fatal failure.";
393 // Tests non-fatal failures in SetUp().
394 class NonFatalFailureInSetUpTest : public testing::Test {
396 virtual ~NonFatalFailureInSetUpTest() {
400 virtual void SetUp() {
401 printf("(expecting 4 failures)\n");
402 ADD_FAILURE() << "Expected failure #1, in SetUp().";
405 virtual void TearDown() {
406 FAIL() << "Expected failure #3, in TearDown().";
410 FAIL() << "Expected failure #4, in the test fixture d'tor.";
414 TEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) {
415 FAIL() << "Expected failure #2, in the test function.";
418 // Tests fatal failures in SetUp().
419 class FatalFailureInSetUpTest : public testing::Test {
421 virtual ~FatalFailureInSetUpTest() {
425 virtual void SetUp() {
426 printf("(expecting 3 failures)\n");
427 FAIL() << "Expected failure #1, in SetUp().";
430 virtual void TearDown() {
431 FAIL() << "Expected failure #2, in TearDown().";
435 FAIL() << "Expected failure #3, in the test fixture d'tor.";
439 TEST_F(FatalFailureInSetUpTest, FailureInSetUp) {
440 FAIL() << "UNEXPECTED failure in the test function. "
441 << "We should never get here, as SetUp() failed.";
444 TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) {
445 ADD_FAILURE_AT("foo.cc", 42) << "Expected failure in foo.cc";
448 #if GTEST_IS_THREADSAFE
450 // A unary function that may die.
451 void DieIf(bool should_die) {
452 GTEST_CHECK_(!should_die) << " - death inside DieIf().";
455 // Tests running death tests in a multi-threaded context.
457 // Used for coordination between the main and the spawn thread.
458 struct SpawnThreadNotifications {
459 SpawnThreadNotifications() {}
461 Notification spawn_thread_started;
462 Notification spawn_thread_ok_to_terminate;
465 GTEST_DISALLOW_COPY_AND_ASSIGN_(SpawnThreadNotifications);
468 // The function to be executed in the thread spawn by the
469 // MultipleThreads test (below).
470 static void ThreadRoutine(SpawnThreadNotifications* notifications) {
471 // Signals the main thread that this thread has started.
472 notifications->spawn_thread_started.Notify();
474 // Waits for permission to finish from the main thread.
475 notifications->spawn_thread_ok_to_terminate.WaitForNotification();
478 // This is a death-test test, but it's not named with a DeathTest
479 // suffix. It starts threads which might interfere with later
480 // death tests, so it must run after all other death tests.
481 class DeathTestAndMultiThreadsTest : public testing::Test {
483 // Starts a thread and waits for it to begin.
484 virtual void SetUp() {
485 thread_.reset(new ThreadWithParam<SpawnThreadNotifications*>(
486 &ThreadRoutine, ¬ifications_, NULL));
487 notifications_.spawn_thread_started.WaitForNotification();
489 // Tells the thread to finish, and reaps it.
490 // Depending on the version of the thread library in use,
491 // a manager thread might still be left running that will interfere
492 // with later death tests. This is unfortunate, but this class
493 // cleans up after itself as best it can.
494 virtual void TearDown() {
495 notifications_.spawn_thread_ok_to_terminate.Notify();
499 SpawnThreadNotifications notifications_;
500 scoped_ptr<ThreadWithParam<SpawnThreadNotifications*> > thread_;
503 #endif // GTEST_IS_THREADSAFE
505 // The MixedUpTestCaseTest test case verifies that Google Test will fail a
506 // test if it uses a different fixture class than what other tests in
507 // the same test case use. It deliberately contains two fixture
508 // classes with the same name but defined in different namespaces.
510 // The MixedUpTestCaseWithSameTestNameTest test case verifies that
511 // when the user defines two tests with the same test case name AND
512 // same test name (but in different namespaces), the second test will
517 class MixedUpTestCaseTest : public testing::Test {
520 TEST_F(MixedUpTestCaseTest, FirstTestFromNamespaceFoo) {}
521 TEST_F(MixedUpTestCaseTest, SecondTestFromNamespaceFoo) {}
523 class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
526 TEST_F(MixedUpTestCaseWithSameTestNameTest,
527 TheSecondTestWithThisNameShouldFail) {}
533 class MixedUpTestCaseTest : public testing::Test {
536 // The following two tests are expected to fail. We rely on the
537 // golden file to check that Google Test generates the right error message.
538 TEST_F(MixedUpTestCaseTest, ThisShouldFail) {}
539 TEST_F(MixedUpTestCaseTest, ThisShouldFailToo) {}
541 class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
544 // Expected to fail. We rely on the golden file to check that Google Test
545 // generates the right error message.
546 TEST_F(MixedUpTestCaseWithSameTestNameTest,
547 TheSecondTestWithThisNameShouldFail) {}
551 // The following two test cases verify that Google Test catches the user
552 // error of mixing TEST and TEST_F in the same test case. The first
553 // test case checks the scenario where TEST_F appears before TEST, and
554 // the second one checks where TEST appears before TEST_F.
556 class TEST_F_before_TEST_in_same_test_case : public testing::Test {
559 TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {}
561 // Expected to fail. We rely on the golden file to check that Google Test
562 // generates the right error message.
563 TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {}
565 class TEST_before_TEST_F_in_same_test_case : public testing::Test {
568 TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {}
570 // Expected to fail. We rely on the golden file to check that Google Test
571 // generates the right error message.
572 TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) {
575 // Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE().
576 int global_integer = 0;
578 // Tests that EXPECT_NONFATAL_FAILURE() can reference global variables.
579 TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
581 EXPECT_NONFATAL_FAILURE({
582 EXPECT_EQ(1, global_integer) << "Expected non-fatal failure.";
583 }, "Expected non-fatal failure.");
586 // Tests that EXPECT_NONFATAL_FAILURE() can reference local variables
588 TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
592 EXPECT_NONFATAL_FAILURE({
593 EXPECT_EQ(m, n) << "Expected non-fatal failure.";
594 }, "Expected non-fatal failure.");
597 // Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly
598 // one non-fatal failure and no fatal failure.
599 TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
600 EXPECT_NONFATAL_FAILURE({
601 ADD_FAILURE() << "Expected non-fatal failure.";
602 }, "Expected non-fatal failure.");
605 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is no
606 // non-fatal failure.
607 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
608 printf("(expecting a failure)\n");
609 EXPECT_NONFATAL_FAILURE({
613 // Tests that EXPECT_NONFATAL_FAILURE() fails when there are two
614 // non-fatal failures.
615 TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
616 printf("(expecting a failure)\n");
617 EXPECT_NONFATAL_FAILURE({
618 ADD_FAILURE() << "Expected non-fatal failure 1.";
619 ADD_FAILURE() << "Expected non-fatal failure 2.";
623 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal
625 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
626 printf("(expecting a failure)\n");
627 EXPECT_NONFATAL_FAILURE({
628 FAIL() << "Expected fatal failure.";
632 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
634 TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
635 printf("(expecting a failure)\n");
636 EXPECT_NONFATAL_FAILURE({
641 #if GTEST_HAS_EXCEPTIONS
643 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
645 TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
646 printf("(expecting a failure)\n");
648 EXPECT_NONFATAL_FAILURE({
651 } catch(int) { // NOLINT
655 #endif // GTEST_HAS_EXCEPTIONS
657 // Tests that EXPECT_FATAL_FAILURE() can reference global variables.
658 TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
660 EXPECT_FATAL_FAILURE({
661 ASSERT_EQ(1, global_integer) << "Expected fatal failure.";
662 }, "Expected fatal failure.");
665 // Tests that EXPECT_FATAL_FAILURE() can reference local static
667 TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
670 EXPECT_FATAL_FAILURE({
671 ASSERT_EQ(0, n) << "Expected fatal failure.";
672 }, "Expected fatal failure.");
675 // Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly
676 // one fatal failure and no non-fatal failure.
677 TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
678 EXPECT_FATAL_FAILURE({
679 FAIL() << "Expected fatal failure.";
680 }, "Expected fatal failure.");
683 // Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal
685 TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
686 printf("(expecting a failure)\n");
687 EXPECT_FATAL_FAILURE({
691 // A helper for generating a fatal failure.
692 void FatalFailure() {
693 FAIL() << "Expected fatal failure.";
696 // Tests that EXPECT_FATAL_FAILURE() fails when there are two
698 TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
699 printf("(expecting a failure)\n");
700 EXPECT_FATAL_FAILURE({
706 // Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal
708 TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
709 printf("(expecting a failure)\n");
710 EXPECT_FATAL_FAILURE({
711 ADD_FAILURE() << "Expected non-fatal failure.";
715 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
717 TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
718 printf("(expecting a failure)\n");
719 EXPECT_FATAL_FAILURE({
724 #if GTEST_HAS_EXCEPTIONS
726 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
728 TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
729 printf("(expecting a failure)\n");
731 EXPECT_FATAL_FAILURE({
734 } catch(int) { // NOLINT
738 #endif // GTEST_HAS_EXCEPTIONS
740 // This #ifdef block tests the output of typed tests.
741 #if GTEST_HAS_TYPED_TEST
743 template <typename T>
744 class TypedTest : public testing::Test {
747 TYPED_TEST_CASE(TypedTest, testing::Types<int>);
749 TYPED_TEST(TypedTest, Success) {
750 EXPECT_EQ(0, TypeParam());
753 TYPED_TEST(TypedTest, Failure) {
754 EXPECT_EQ(1, TypeParam()) << "Expected failure";
757 #endif // GTEST_HAS_TYPED_TEST
759 // This #ifdef block tests the output of type-parameterized tests.
760 #if GTEST_HAS_TYPED_TEST_P
762 template <typename T>
763 class TypedTestP : public testing::Test {
766 TYPED_TEST_CASE_P(TypedTestP);
768 TYPED_TEST_P(TypedTestP, Success) {
769 EXPECT_EQ(0U, TypeParam());
772 TYPED_TEST_P(TypedTestP, Failure) {
773 EXPECT_EQ(1U, TypeParam()) << "Expected failure";
776 REGISTER_TYPED_TEST_CASE_P(TypedTestP, Success, Failure);
778 typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
779 INSTANTIATE_TYPED_TEST_CASE_P(Unsigned, TypedTestP, UnsignedTypes);
781 #endif // GTEST_HAS_TYPED_TEST_P
783 #if GTEST_HAS_DEATH_TEST
785 // We rely on the golden file to verify that tests whose test case
786 // name ends with DeathTest are run first.
788 TEST(ADeathTest, ShouldRunFirst) {
791 # if GTEST_HAS_TYPED_TEST
793 // We rely on the golden file to verify that typed tests whose test
794 // case name ends with DeathTest are run first.
796 template <typename T>
797 class ATypedDeathTest : public testing::Test {
800 typedef testing::Types<int, double> NumericTypes;
801 TYPED_TEST_CASE(ATypedDeathTest, NumericTypes);
803 TYPED_TEST(ATypedDeathTest, ShouldRunFirst) {
806 # endif // GTEST_HAS_TYPED_TEST
808 # if GTEST_HAS_TYPED_TEST_P
811 // We rely on the golden file to verify that type-parameterized tests
812 // whose test case name ends with DeathTest are run first.
814 template <typename T>
815 class ATypeParamDeathTest : public testing::Test {
818 TYPED_TEST_CASE_P(ATypeParamDeathTest);
820 TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) {
823 REGISTER_TYPED_TEST_CASE_P(ATypeParamDeathTest, ShouldRunFirst);
825 INSTANTIATE_TYPED_TEST_CASE_P(My, ATypeParamDeathTest, NumericTypes);
827 # endif // GTEST_HAS_TYPED_TEST_P
829 #endif // GTEST_HAS_DEATH_TEST
831 // Tests various failure conditions of
832 // EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}.
833 class ExpectFailureTest : public testing::Test {
834 public: // Must be public and not protected due to a bug in g++ 3.4.2.
839 static void AddFailure(FailureMode failure) {
840 if (failure == FATAL_FAILURE) {
841 FAIL() << "Expected fatal failure.";
843 ADD_FAILURE() << "Expected non-fatal failure.";
848 TEST_F(ExpectFailureTest, ExpectFatalFailure) {
849 // Expected fatal failure, but succeeds.
850 printf("(expecting 1 failure)\n");
851 EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure.");
852 // Expected fatal failure, but got a non-fatal failure.
853 printf("(expecting 1 failure)\n");
854 EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Expected non-fatal "
857 printf("(expecting 1 failure)\n");
858 EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), "Some other fatal failure "
862 TEST_F(ExpectFailureTest, ExpectNonFatalFailure) {
863 // Expected non-fatal failure, but succeeds.
864 printf("(expecting 1 failure)\n");
865 EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure.");
866 // Expected non-fatal failure, but got a fatal failure.
867 printf("(expecting 1 failure)\n");
868 EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure.");
870 printf("(expecting 1 failure)\n");
871 EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Some other non-fatal "
875 #if GTEST_IS_THREADSAFE
877 class ExpectFailureWithThreadsTest : public ExpectFailureTest {
879 static void AddFailureInOtherThread(FailureMode failure) {
880 ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL);
885 TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
886 // We only intercept the current thread.
887 printf("(expecting 2 failures)\n");
888 EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE),
889 "Expected fatal failure.");
892 TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
893 // We only intercept the current thread.
894 printf("(expecting 2 failures)\n");
895 EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE),
896 "Expected non-fatal failure.");
899 typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
901 // Tests that the ScopedFakeTestPartResultReporter only catches failures from
902 // the current thread if it is instantiated with INTERCEPT_ONLY_CURRENT_THREAD.
903 TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
904 printf("(expecting 2 failures)\n");
905 TestPartResultArray results;
907 ScopedFakeTestPartResultReporter reporter(
908 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
910 AddFailureInOtherThread(FATAL_FAILURE);
911 AddFailureInOtherThread(NONFATAL_FAILURE);
913 // The two failures should not have been intercepted.
914 EXPECT_EQ(0, results.size()) << "This shouldn't fail.";
917 #endif // GTEST_IS_THREADSAFE
919 TEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) {
920 // Expected fatal failure, but succeeds.
921 printf("(expecting 1 failure)\n");
922 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure.");
923 // Expected fatal failure, but got a non-fatal failure.
924 printf("(expecting 1 failure)\n");
925 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
926 "Expected non-fatal failure.");
928 printf("(expecting 1 failure)\n");
929 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
930 "Some other fatal failure expected.");
933 TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) {
934 // Expected non-fatal failure, but succeeds.
935 printf("(expecting 1 failure)\n");
936 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected non-fatal "
938 // Expected non-fatal failure, but got a fatal failure.
939 printf("(expecting 1 failure)\n");
940 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
941 "Expected fatal failure.");
943 printf("(expecting 1 failure)\n");
944 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
945 "Some other non-fatal failure.");
949 // Two test environments for testing testing::AddGlobalTestEnvironment().
951 class FooEnvironment : public testing::Environment {
953 virtual void SetUp() {
954 printf("%s", "FooEnvironment::SetUp() called.\n");
957 virtual void TearDown() {
958 printf("%s", "FooEnvironment::TearDown() called.\n");
959 FAIL() << "Expected fatal failure.";
963 class BarEnvironment : public testing::Environment {
965 virtual void SetUp() {
966 printf("%s", "BarEnvironment::SetUp() called.\n");
969 virtual void TearDown() {
970 printf("%s", "BarEnvironment::TearDown() called.\n");
971 ADD_FAILURE() << "Expected non-fatal failure.";
975 bool GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = false;
977 // The main function.
979 // The idea is to use Google Test to run all the tests we have defined (some
980 // of them are intended to fail), and then compare the test results
981 // with the "golden" file.
982 int main(int argc, char **argv) {
983 testing::GTEST_FLAG(print_time) = false;
985 // We just run the tests, knowing some of them are intended to fail.
986 // We will use a separate Python script to compare the output of
987 // this program with the golden file.
989 // It's hard to test InitGoogleTest() directly, as it has many
990 // global side effects. The following line serves as a sanity test
992 testing::InitGoogleTest(&argc, argv);
994 String(argv[1]) == "--gtest_internal_skip_environment_and_ad_hoc_tests")
995 GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = true;
997 #if GTEST_HAS_DEATH_TEST
998 if (testing::internal::GTEST_FLAG(internal_run_death_test) != "") {
999 // Skip the usual output capturing if we're running as the child
1000 // process of an threadsafe-style death test.
1001 # if GTEST_OS_WINDOWS
1002 posix::FReopen("nul:", "w", stdout);
1004 posix::FReopen("/dev/null", "w", stdout);
1005 # endif // GTEST_OS_WINDOWS
1006 return RUN_ALL_TESTS();
1008 #endif // GTEST_HAS_DEATH_TEST
1010 if (GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests))
1011 return RUN_ALL_TESTS();
1013 // Registers two global test environments.
1014 // The golden file verifies that they are set up in the order they
1015 // are registered, and torn down in the reverse order.
1016 testing::AddGlobalTestEnvironment(new FooEnvironment);
1017 testing::AddGlobalTestEnvironment(new BarEnvironment);
1019 return RunAllTests();