source: gtest-1.7.0/samples/sample9_unittest.cc@ 12770

Last change on this file since 12770 was 12746, checked in by hock@…, 11 years ago

integrated the Google Testing Framework (gtest)

and wrote an Hello World test, to ensure the framework is working..

File size: 5.8 KB
Line 
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// Author: vladl@google.com (Vlad Losev)
30
31// This sample shows how to use Google Test listener API to implement
32// an alternative console output and how to use the UnitTest reflection API
33// to enumerate test cases and tests and to inspect their results.
34
35#include <stdio.h>
36
37#include "gtest/gtest.h"
38
39using ::testing::EmptyTestEventListener;
40using ::testing::InitGoogleTest;
41using ::testing::Test;
42using ::testing::TestCase;
43using ::testing::TestEventListeners;
44using ::testing::TestInfo;
45using ::testing::TestPartResult;
46using ::testing::UnitTest;
47
48namespace {
49
50// Provides alternative output mode which produces minimal amount of
51// information about tests.
52class TersePrinter : public EmptyTestEventListener {
53 private:
54 // Called before any test activity starts.
55 virtual void OnTestProgramStart(const UnitTest& /* unit_test */) {}
56
57 // Called after all test activities have ended.
58 virtual void OnTestProgramEnd(const UnitTest& unit_test) {
59 fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED");
60 fflush(stdout);
61 }
62
63 // Called before a test starts.
64 virtual void OnTestStart(const TestInfo& test_info) {
65 fprintf(stdout,
66 "*** Test %s.%s starting.\n",
67 test_info.test_case_name(),
68 test_info.name());
69 fflush(stdout);
70 }
71
72 // Called after a failed assertion or a SUCCEED() invocation.
73 virtual void OnTestPartResult(const TestPartResult& test_part_result) {
74 fprintf(stdout,
75 "%s in %s:%d\n%s\n",
76 test_part_result.failed() ? "*** Failure" : "Success",
77 test_part_result.file_name(),
78 test_part_result.line_number(),
79 test_part_result.summary());
80 fflush(stdout);
81 }
82
83 // Called after a test ends.
84 virtual void OnTestEnd(const TestInfo& test_info) {
85 fprintf(stdout,
86 "*** Test %s.%s ending.\n",
87 test_info.test_case_name(),
88 test_info.name());
89 fflush(stdout);
90 }
91}; // class TersePrinter
92
93TEST(CustomOutputTest, PrintsMessage) {
94 printf("Printing something from the test body...\n");
95}
96
97TEST(CustomOutputTest, Succeeds) {
98 SUCCEED() << "SUCCEED() has been invoked from here";
99}
100
101TEST(CustomOutputTest, Fails) {
102 EXPECT_EQ(1, 2)
103 << "This test fails in order to demonstrate alternative failure messages";
104}
105
106} // namespace
107
108int main(int argc, char **argv) {
109 InitGoogleTest(&argc, argv);
110
111 bool terse_output = false;
112 if (argc > 1 && strcmp(argv[1], "--terse_output") == 0 )
113 terse_output = true;
114 else
115 printf("%s\n", "Run this program with --terse_output to change the way "
116 "it prints its output.");
117
118 UnitTest& unit_test = *UnitTest::GetInstance();
119
120 // If we are given the --terse_output command line flag, suppresses the
121 // standard output and attaches own result printer.
122 if (terse_output) {
123 TestEventListeners& listeners = unit_test.listeners();
124
125 // Removes the default console output listener from the list so it will
126 // not receive events from Google Test and won't print any output. Since
127 // this operation transfers ownership of the listener to the caller we
128 // have to delete it as well.
129 delete listeners.Release(listeners.default_result_printer());
130
131 // Adds the custom output listener to the list. It will now receive
132 // events from Google Test and print the alternative output. We don't
133 // have to worry about deleting it since Google Test assumes ownership
134 // over it after adding it to the list.
135 listeners.Append(new TersePrinter);
136 }
137 int ret_val = RUN_ALL_TESTS();
138
139 // This is an example of using the UnitTest reflection API to inspect test
140 // results. Here we discount failures from the tests we expected to fail.
141 int unexpectedly_failed_tests = 0;
142 for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
143 const TestCase& test_case = *unit_test.GetTestCase(i);
144 for (int j = 0; j < test_case.total_test_count(); ++j) {
145 const TestInfo& test_info = *test_case.GetTestInfo(j);
146 // Counts failed tests that were not meant to fail (those without
147 // 'Fails' in the name).
148 if (test_info.result()->Failed() &&
149 strcmp(test_info.name(), "Fails") != 0) {
150 unexpectedly_failed_tests++;
151 }
152 }
153 }
154
155 // Test that were meant to fail should not affect the test program outcome.
156 if (unexpectedly_failed_tests == 0)
157 ret_val = 0;
158
159 return ret_val;
160}
Note: See TracBrowser for help on using the repository browser.