proxygen
sample9_unittest.cc File Reference
#include <stdio.h>
#include "gtest/gtest.h"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 108 of file sample9_unittest.cc.

References i, testing::InitGoogleTest(), RUN_ALL_TESTS(), and gmock_test_utils::TestCase.

108  {
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 }
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2232
char ** argv
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5370