sample9_unittest.cc
Go to the documentation of this file.
00001 // Copyright 2009 Google Inc. All Rights Reserved.
00002 //
00003 // Redistribution and use in source and binary forms, with or without
00004 // modification, are permitted provided that the following conditions are
00005 // met:
00006 //
00007 //     * Redistributions of source code must retain the above copyright
00008 // notice, this list of conditions and the following disclaimer.
00009 //     * Redistributions in binary form must reproduce the above
00010 // copyright notice, this list of conditions and the following disclaimer
00011 // in the documentation and/or other materials provided with the
00012 // distribution.
00013 //     * Neither the name of Google Inc. nor the names of its
00014 // contributors may be used to endorse or promote products derived from
00015 // this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00018 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00020 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00021 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00022 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00024 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00025 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00027 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 //
00029 // Author: vladl@google.com (Vlad Losev)
00030 
00031 // This sample shows how to use Google Test listener API to implement
00032 // an alternative console output and how to use the UnitTest reflection API
00033 // to enumerate test cases and tests and to inspect their results.
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 // Provides alternative output mode which produces minimal amount of
00051 // information about tests.
00052 class TersePrinter : public EmptyTestEventListener {
00053  private:
00054   // Called before any test activity starts.
00055   virtual void OnTestProgramStart(const UnitTest& /* unit_test */) {}
00056 
00057   // Called after all test activities have ended.
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   // Called before a test starts.
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   // Called after a failed assertion or a SUCCEED() invocation.
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   // Called after a test ends.
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 };  // class TersePrinter
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 }  // namespace
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   // If we are given the --terse_output command line flag, suppresses the
00121   // standard output and attaches own result printer.
00122   if (terse_output) {
00123     TestEventListeners& listeners = unit_test.listeners();
00124 
00125     // Removes the default console output listener from the list so it will
00126     // not receive events from Google Test and won't print any output. Since
00127     // this operation transfers ownership of the listener to the caller we
00128     // have to delete it as well.
00129     delete listeners.Release(listeners.default_result_printer());
00130 
00131     // Adds the custom output listener to the list. It will now receive
00132     // events from Google Test and print the alternative output. We don't
00133     // have to worry about deleting it since Google Test assumes ownership
00134     // over it after adding it to the list.
00135     listeners.Append(new TersePrinter);
00136   }
00137   int ret_val = RUN_ALL_TESTS();
00138 
00139   // This is an example of using the UnitTest reflection API to inspect test
00140   // results. Here we discount failures from the tests we expected to fail.
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       // Counts failed tests that were not meant to fail (those without
00147       // 'Fails' in the name).
00148       if (test_info.result()->Failed() &&
00149           strcmp(test_info.name(), "Fails") != 0) {
00150         unexpectedly_failed_tests++;
00151       }
00152     }
00153   }
00154 
00155   // Test that were meant to fail should not affect the test program outcome.
00156   if (unexpectedly_failed_tests == 0)
00157     ret_val = 0;
00158 
00159   return ret_val;
00160 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06