Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <stdio.h>
00036
00037 #include "gtest/gtest.h"
00038
00039 using ::testing::EmptyTestEventListener;
00040 using ::testing::InitGoogleTest;
00041 using ::testing::Test;
00042 using ::testing::TestCase;
00043 using ::testing::TestEventListeners;
00044 using ::testing::TestInfo;
00045 using ::testing::TestPartResult;
00046 using ::testing::UnitTest;
00047
00048 namespace {
00049
00050
00051
00052 class TersePrinter : public EmptyTestEventListener {
00053 private:
00054
00055 virtual void OnTestProgramStart(const UnitTest& ) {}
00056
00057
00058 virtual void OnTestProgramEnd(const UnitTest& unit_test) {
00059 fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED");
00060 fflush(stdout);
00061 }
00062
00063
00064 virtual void OnTestStart(const TestInfo& test_info) {
00065 fprintf(stdout,
00066 "*** Test %s.%s starting.\n",
00067 test_info.test_case_name(),
00068 test_info.name());
00069 fflush(stdout);
00070 }
00071
00072
00073 virtual void OnTestPartResult(const TestPartResult& test_part_result) {
00074 fprintf(stdout,
00075 "%s in %s:%d\n%s\n",
00076 test_part_result.failed() ? "*** Failure" : "Success",
00077 test_part_result.file_name(),
00078 test_part_result.line_number(),
00079 test_part_result.summary());
00080 fflush(stdout);
00081 }
00082
00083
00084 virtual void OnTestEnd(const TestInfo& test_info) {
00085 fprintf(stdout,
00086 "*** Test %s.%s ending.\n",
00087 test_info.test_case_name(),
00088 test_info.name());
00089 fflush(stdout);
00090 }
00091 };
00092
00093 TEST(CustomOutputTest, PrintsMessage) {
00094 printf("Printing something from the test body...\n");
00095 }
00096
00097 TEST(CustomOutputTest, Succeeds) {
00098 SUCCEED() << "SUCCEED() has been invoked from here";
00099 }
00100
00101 TEST(CustomOutputTest, Fails) {
00102 EXPECT_EQ(1, 2)
00103 << "This test fails in order to demonstrate alternative failure messages";
00104 }
00105
00106 }
00107
00108 int main(int argc, char **argv) {
00109 InitGoogleTest(&argc, argv);
00110
00111 bool terse_output = false;
00112 if (argc > 1 && strcmp(argv[1], "--terse_output") == 0 )
00113 terse_output = true;
00114 else
00115 printf("%s\n", "Run this program with --terse_output to change the way "
00116 "it prints its output.");
00117
00118 UnitTest& unit_test = *UnitTest::GetInstance();
00119
00120
00121
00122 if (terse_output) {
00123 TestEventListeners& listeners = unit_test.listeners();
00124
00125
00126
00127
00128
00129 delete listeners.Release(listeners.default_result_printer());
00130
00131
00132
00133
00134
00135 listeners.Append(new TersePrinter);
00136 }
00137 int ret_val = RUN_ALL_TESTS();
00138
00139
00140
00141 int unexpectedly_failed_tests = 0;
00142 for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
00143 const TestCase& test_case = *unit_test.GetTestCase(i);
00144 for (int j = 0; j < test_case.total_test_count(); ++j) {
00145 const TestInfo& test_info = *test_case.GetTestInfo(j);
00146
00147
00148 if (test_info.result()->Failed() &&
00149 strcmp(test_info.name(), "Fails") != 0) {
00150 unexpectedly_failed_tests++;
00151 }
00152 }
00153 }
00154
00155
00156 if (unexpectedly_failed_tests == 0)
00157 ret_val = 0;
00158
00159 return ret_val;
00160 }