michael@0: // Copyright 2009 Google Inc. All Rights Reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // Author: vladl@google.com (Vlad Losev) michael@0: michael@0: // This sample shows how to use Google Test listener API to implement michael@0: // an alternative console output and how to use the UnitTest reflection API michael@0: // to enumerate test cases and tests and to inspect their results. michael@0: michael@0: #include michael@0: michael@0: #include "gtest/gtest.h" michael@0: michael@0: using ::testing::EmptyTestEventListener; michael@0: using ::testing::InitGoogleTest; michael@0: using ::testing::Test; michael@0: using ::testing::TestCase; michael@0: using ::testing::TestEventListeners; michael@0: using ::testing::TestInfo; michael@0: using ::testing::TestPartResult; michael@0: using ::testing::UnitTest; michael@0: michael@0: namespace { michael@0: michael@0: // Provides alternative output mode which produces minimal amount of michael@0: // information about tests. michael@0: class TersePrinter : public EmptyTestEventListener { michael@0: private: michael@0: // Called before any test activity starts. michael@0: virtual void OnTestProgramStart(const UnitTest& /* unit_test */) {} michael@0: michael@0: // Called after all test activities have ended. michael@0: virtual void OnTestProgramEnd(const UnitTest& unit_test) { michael@0: fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED"); michael@0: fflush(stdout); michael@0: } michael@0: michael@0: // Called before a test starts. michael@0: virtual void OnTestStart(const TestInfo& test_info) { michael@0: fprintf(stdout, michael@0: "*** Test %s.%s starting.\n", michael@0: test_info.test_case_name(), michael@0: test_info.name()); michael@0: fflush(stdout); michael@0: } michael@0: michael@0: // Called after a failed assertion or a SUCCEED() invocation. michael@0: virtual void OnTestPartResult(const TestPartResult& test_part_result) { michael@0: fprintf(stdout, michael@0: "%s in %s:%d\n%s\n", michael@0: test_part_result.failed() ? "*** Failure" : "Success", michael@0: test_part_result.file_name(), michael@0: test_part_result.line_number(), michael@0: test_part_result.summary()); michael@0: fflush(stdout); michael@0: } michael@0: michael@0: // Called after a test ends. michael@0: virtual void OnTestEnd(const TestInfo& test_info) { michael@0: fprintf(stdout, michael@0: "*** Test %s.%s ending.\n", michael@0: test_info.test_case_name(), michael@0: test_info.name()); michael@0: fflush(stdout); michael@0: } michael@0: }; // class TersePrinter michael@0: michael@0: TEST(CustomOutputTest, PrintsMessage) { michael@0: printf("Printing something from the test body...\n"); michael@0: } michael@0: michael@0: TEST(CustomOutputTest, Succeeds) { michael@0: SUCCEED() << "SUCCEED() has been invoked from here"; michael@0: } michael@0: michael@0: TEST(CustomOutputTest, Fails) { michael@0: EXPECT_EQ(1, 2) michael@0: << "This test fails in order to demonstrate alternative failure messages"; michael@0: } michael@0: michael@0: } // namespace michael@0: michael@0: int main(int argc, char **argv) { michael@0: InitGoogleTest(&argc, argv); michael@0: michael@0: bool terse_output = false; michael@0: if (argc > 1 && strcmp(argv[1], "--terse_output") == 0 ) michael@0: terse_output = true; michael@0: else michael@0: printf("%s\n", "Run this program with --terse_output to change the way " michael@0: "it prints its output."); michael@0: michael@0: UnitTest& unit_test = *UnitTest::GetInstance(); michael@0: michael@0: // If we are given the --terse_output command line flag, suppresses the michael@0: // standard output and attaches own result printer. michael@0: if (terse_output) { michael@0: TestEventListeners& listeners = unit_test.listeners(); michael@0: michael@0: // Removes the default console output listener from the list so it will michael@0: // not receive events from Google Test and won't print any output. Since michael@0: // this operation transfers ownership of the listener to the caller we michael@0: // have to delete it as well. michael@0: delete listeners.Release(listeners.default_result_printer()); michael@0: michael@0: // Adds the custom output listener to the list. It will now receive michael@0: // events from Google Test and print the alternative output. We don't michael@0: // have to worry about deleting it since Google Test assumes ownership michael@0: // over it after adding it to the list. michael@0: listeners.Append(new TersePrinter); michael@0: } michael@0: int ret_val = RUN_ALL_TESTS(); michael@0: michael@0: // This is an example of using the UnitTest reflection API to inspect test michael@0: // results. Here we discount failures from the tests we expected to fail. michael@0: int unexpectedly_failed_tests = 0; michael@0: for (int i = 0; i < unit_test.total_test_case_count(); ++i) { michael@0: const TestCase& test_case = *unit_test.GetTestCase(i); michael@0: for (int j = 0; j < test_case.total_test_count(); ++j) { michael@0: const TestInfo& test_info = *test_case.GetTestInfo(j); michael@0: // Counts failed tests that were not meant to fail (those without michael@0: // 'Fails' in the name). michael@0: if (test_info.result()->Failed() && michael@0: strcmp(test_info.name(), "Fails") != 0) { michael@0: unexpectedly_failed_tests++; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Test that were meant to fail should not affect the test program outcome. michael@0: if (unexpectedly_failed_tests == 0) michael@0: ret_val = 0; michael@0: michael@0: return ret_val; michael@0: }