F´ Flight Software - C/C++ Documentation NASA-v1.6.0
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sample9_unittest.cc
Go to the documentation of this file.
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
37using ::testing::EmptyTestEventListener;
38using ::testing::InitGoogleTest;
39using ::testing::Test;
40using ::testing::TestEventListeners;
41using ::testing::TestInfo;
42using ::testing::TestPartResult;
43using ::testing::TestSuite;
44using ::testing::UnitTest;
45namespace {
46// Provides alternative output mode which produces minimal amount of
47// information about tests.
48class TersePrinter : public EmptyTestEventListener {
49 private:
50 // Called before any test activity starts.
51 void OnTestProgramStart(const UnitTest& /* unit_test */) override {}
52
53 // Called after all test activities have ended.
54 void OnTestProgramEnd(const UnitTest& unit_test) override {
55 fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED");
56 fflush(stdout);
57 }
58
59 // Called before a test starts.
60 void OnTestStart(const TestInfo& test_info) override {
61 fprintf(stdout, "*** Test %s.%s starting.\n", test_info.test_suite_name(),
62 test_info.name());
63 fflush(stdout);
64 }
65
66 // Called after a failed assertion or a SUCCEED() invocation.
67 void OnTestPartResult(const TestPartResult& test_part_result) override {
68 fprintf(stdout, "%s in %s:%d\n%s\n",
69 test_part_result.failed() ? "*** Failure" : "Success",
70 test_part_result.file_name(), test_part_result.line_number(),
71 test_part_result.summary());
72 fflush(stdout);
73 }
74
75 // Called after a test ends.
76 void OnTestEnd(const TestInfo& test_info) override {
77 fprintf(stdout, "*** Test %s.%s ending.\n", test_info.test_suite_name(),
78 test_info.name());
79 fflush(stdout);
80 }
81}; // class TersePrinter
82
83TEST(CustomOutputTest, PrintsMessage) {
84 printf("Printing something from the test body...\n");
85}
86
87TEST(CustomOutputTest, Succeeds) {
88 SUCCEED() << "SUCCEED() has been invoked from here";
89}
90
91TEST(CustomOutputTest, Fails) {
92 EXPECT_EQ(1, 2)
93 << "This test fails in order to demonstrate alternative failure messages";
94}
95} // namespace
96
97int main(int argc, char** argv) {
98 InitGoogleTest(&argc, argv);
99
100 bool terse_output = false;
101 if (argc > 1 && strcmp(argv[1], "--terse_output") == 0)
102 terse_output = true;
103 else
104 printf("%s\n",
105 "Run this program with --terse_output to change the way "
106 "it prints its output.");
107
108 UnitTest& unit_test = *UnitTest::GetInstance();
109
110 // If we are given the --terse_output command line flag, suppresses the
111 // standard output and attaches own result printer.
112 if (terse_output) {
113 TestEventListeners& listeners = unit_test.listeners();
114
115 // Removes the default console output listener from the list so it will
116 // not receive events from Google Test and won't print any output. Since
117 // this operation transfers ownership of the listener to the caller we
118 // have to delete it as well.
119 delete listeners.Release(listeners.default_result_printer());
120
121 // Adds the custom output listener to the list. It will now receive
122 // events from Google Test and print the alternative output. We don't
123 // have to worry about deleting it since Google Test assumes ownership
124 // over it after adding it to the list.
125 listeners.Append(new TersePrinter);
126 }
127 int ret_val = RUN_ALL_TESTS();
128
129 // This is an example of using the UnitTest reflection API to inspect test
130 // results. Here we discount failures from the tests we expected to fail.
131 int unexpectedly_failed_tests = 0;
132 for (int i = 0; i < unit_test.total_test_suite_count(); ++i) {
133 const testing::TestSuite& test_suite = *unit_test.GetTestSuite(i);
134 for (int j = 0; j < test_suite.total_test_count(); ++j) {
135 const TestInfo& test_info = *test_suite.GetTestInfo(j);
136 // Counts failed tests that were not meant to fail (those without
137 // 'Fails' in the name).
138 if (test_info.result()->Failed() &&
139 strcmp(test_info.name(), "Fails") != 0) {
140 unexpectedly_failed_tests++;
141 }
142 }
143 }
144
145 // Test that were meant to fail should not affect the test program outcome.
146 if (unexpectedly_failed_tests == 0) ret_val = 0;
147
148 return ret_val;
149}
TEST(ComponentParameterTest, ParameterTest)
Definition Tests.cpp:91
int main(int argc, char **argv)