1 // Copyright 2009 Google Inc. All Rights Reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google Inc. nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 // This sample shows how to use Google Test listener API to implement
30 // an alternative console output and how to use the UnitTest reflection API
31 // to enumerate test suites and tests and to inspect their results.
32
33 #include <stdio.h>
34
35 #include "gtest/gtest.h"
36
37 using ::testing::EmptyTestEventListener;
38 using ::testing::InitGoogleTest;
39 using ::testing::Test;
40 using ::testing::TestEventListeners;
41 using ::testing::TestInfo;
42 using ::testing::TestPartResult;
43 using ::testing::UnitTest;
44 namespace {
45 // Provides alternative output mode which produces minimal amount of
46 // information about tests.
47 class TersePrinter : public EmptyTestEventListener {
48 private:
49 // Called before any test activity starts.
OnTestProgramStart(const UnitTest &)50 void OnTestProgramStart(const UnitTest& /* unit_test */) override {}
51
52 // Called after all test activities have ended.
OnTestProgramEnd(const UnitTest & unit_test)53 void OnTestProgramEnd(const UnitTest& unit_test) override {
54 fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED");
55 fflush(stdout);
56 }
57
58 // Called before a test starts.
OnTestStart(const TestInfo & test_info)59 void OnTestStart(const TestInfo& test_info) override {
60 fprintf(stdout, "*** Test %s.%s starting.\n", test_info.test_suite_name(),
61 test_info.name());
62 fflush(stdout);
63 }
64
65 // Called after a failed assertion or a SUCCEED() invocation.
OnTestPartResult(const TestPartResult & test_part_result)66 void OnTestPartResult(const TestPartResult& test_part_result) override {
67 fprintf(stdout, "%s in %s:%d\n%s\n",
68 test_part_result.failed() ? "*** Failure" : "Success",
69 test_part_result.file_name(), test_part_result.line_number(),
70 test_part_result.summary());
71 fflush(stdout);
72 }
73
74 // Called after a test ends.
OnTestEnd(const TestInfo & test_info)75 void OnTestEnd(const TestInfo& test_info) override {
76 fprintf(stdout, "*** Test %s.%s ending.\n", test_info.test_suite_name(),
77 test_info.name());
78 fflush(stdout);
79 }
80 }; // class TersePrinter
81
TEST(CustomOutputTest,PrintsMessage)82 TEST(CustomOutputTest, PrintsMessage) {
83 printf("Printing something from the test body...\n");
84 }
85
TEST(CustomOutputTest,Succeeds)86 TEST(CustomOutputTest, Succeeds) {
87 SUCCEED() << "SUCCEED() has been invoked from here";
88 }
89
TEST(CustomOutputTest,Fails)90 TEST(CustomOutputTest, Fails) {
91 EXPECT_EQ(1, 2)
92 << "This test fails in order to demonstrate alternative failure messages";
93 }
94 } // namespace
95
main(int argc,char ** argv)96 int main(int argc, char** argv) {
97 InitGoogleTest(&argc, argv);
98
99 bool terse_output = false;
100 if (argc > 1 && strcmp(argv[1], "--terse_output") == 0)
101 terse_output = true;
102 else
103 printf("%s\n",
104 "Run this program with --terse_output to change the way "
105 "it prints its output.");
106
107 UnitTest& unit_test = *UnitTest::GetInstance();
108
109 // If we are given the --terse_output command line flag, suppresses the
110 // standard output and attaches own result printer.
111 if (terse_output) {
112 TestEventListeners& listeners = unit_test.listeners();
113
114 // Removes the default console output listener from the list so it will
115 // not receive events from Google Test and won't print any output. Since
116 // this operation transfers ownership of the listener to the caller we
117 // have to delete it as well.
118 delete listeners.Release(listeners.default_result_printer());
119
120 // Adds the custom output listener to the list. It will now receive
121 // events from Google Test and print the alternative output. We don't
122 // have to worry about deleting it since Google Test assumes ownership
123 // over it after adding it to the list.
124 listeners.Append(new TersePrinter);
125 }
126 int ret_val = RUN_ALL_TESTS();
127
128 // This is an example of using the UnitTest reflection API to inspect test
129 // results. Here we discount failures from the tests we expected to fail.
130 int unexpectedly_failed_tests = 0;
131 for (int i = 0; i < unit_test.total_test_suite_count(); ++i) {
132 const testing::TestSuite& test_suite = *unit_test.GetTestSuite(i);
133 for (int j = 0; j < test_suite.total_test_count(); ++j) {
134 const TestInfo& test_info = *test_suite.GetTestInfo(j);
135 // Counts failed tests that were not meant to fail (those without
136 // 'Fails' in the name).
137 if (test_info.result()->Failed() &&
138 strcmp(test_info.name(), "Fails") != 0) {
139 unexpectedly_failed_tests++;
140 }
141 }
142 }
143
144 // Test that were meant to fail should not affect the test program outcome.
145 if (unexpectedly_failed_tests == 0) ret_val = 0;
146
147 return ret_val;
148 }
149