1d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// Copyright 2009 Google Inc. All Rights Reserved. 2d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// 3d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// Redistribution and use in source and binary forms, with or without 4d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// modification, are permitted provided that the following conditions are 5d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// met: 6d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// 7d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// * Redistributions of source code must retain the above copyright 8d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// notice, this list of conditions and the following disclaimer. 9d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// * Redistributions in binary form must reproduce the above 10d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// copyright notice, this list of conditions and the following disclaimer 11d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// in the documentation and/or other materials provided with the 12d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// distribution. 13d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// * Neither the name of Google Inc. nor the names of its 14d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// contributors may be used to endorse or promote products derived from 15d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// this software without specific prior written permission. 16d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// 17d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// 29d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// Author: vladl@google.com (Vlad Losev) 30d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 31d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// This sample shows how to use Google Test listener API to implement 32d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// an alternative console output and how to use the UnitTest reflection API 33d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// to enumerate test cases and tests and to inspect their results. 34d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 35d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan#include <stdio.h> 36d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 372620c79810d4741922e9fa89050c0af564994f24zhanyong.wan#include "gtest/gtest.h" 38d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 39fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wanusing ::testing::EmptyTestEventListener; 40d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wanusing ::testing::InitGoogleTest; 41d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wanusing ::testing::Test; 42fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wanusing ::testing::TestCase; 43f6d087b78d230d875bf5d8281112662795044680zhanyong.wanusing ::testing::TestEventListeners; 44d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wanusing ::testing::TestInfo; 45d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wanusing ::testing::TestPartResult; 46d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wanusing ::testing::UnitTest; 47d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 48d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wannamespace { 49d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 50d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// Provides alternative output mode which produces minimal amount of 51d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan// information about tests. 52d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wanclass TersePrinter : public EmptyTestEventListener { 53d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan private: 54d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // Called before any test activity starts. 55a6b964f85b615360d42aa68658621a0eaf9ec225zhanyong.wan virtual void OnTestProgramStart(const UnitTest& /* unit_test */) {} 56d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 57d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // Called after all test activities have ended. 58d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan virtual void OnTestProgramEnd(const UnitTest& unit_test) { 59fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED"); 60d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan fflush(stdout); 61d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan } 62d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 63d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // Called before a test starts. 64d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan virtual void OnTestStart(const TestInfo& test_info) { 65d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan fprintf(stdout, 66d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan "*** Test %s.%s starting.\n", 67d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan test_info.test_case_name(), 68d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan test_info.name()); 69d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan fflush(stdout); 70d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan } 71d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 72e974309f2322ed19a0239e6247d806c156e00986vladlosev // Called after a failed assertion or a SUCCEED() invocation. 737dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan virtual void OnTestPartResult(const TestPartResult& test_part_result) { 74d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan fprintf(stdout, 75d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan "%s in %s:%d\n%s\n", 76d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan test_part_result.failed() ? "*** Failure" : "Success", 77d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan test_part_result.file_name(), 78d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan test_part_result.line_number(), 79d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan test_part_result.summary()); 80d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan fflush(stdout); 81d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan } 82334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan 83334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan // Called after a test ends. 84334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan virtual void OnTestEnd(const TestInfo& test_info) { 85334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan fprintf(stdout, 86334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan "*** Test %s.%s ending.\n", 87334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan test_info.test_case_name(), 88334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan test_info.name()); 89334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan fflush(stdout); 90334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan } 91d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan}; // class TersePrinter 92d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 93d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wanTEST(CustomOutputTest, PrintsMessage) { 94d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan printf("Printing something from the test body...\n"); 95d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan} 96d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 97d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wanTEST(CustomOutputTest, Succeeds) { 98d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan SUCCEED() << "SUCCEED() has been invoked from here"; 99d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan} 100d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 101d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wanTEST(CustomOutputTest, Fails) { 102d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan EXPECT_EQ(1, 2) 103d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan << "This test fails in order to demonstrate alternative failure messages"; 104d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan} 105d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 106d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan} // namespace 107d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 108d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wanint main(int argc, char **argv) { 109d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan InitGoogleTest(&argc, argv); 110d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 111d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan bool terse_output = false; 112d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan if (argc > 1 && strcmp(argv[1], "--terse_output") == 0 ) 113d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan terse_output = true; 114d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan else 115d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan printf("%s\n", "Run this program with --terse_output to change the way " 116d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan "it prints its output."); 117d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 118fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan UnitTest& unit_test = *UnitTest::GetInstance(); 119fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan 120d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // If we are given the --terse_output command line flag, suppresses the 121d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // standard output and attaches own result printer. 122d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan if (terse_output) { 123f6d087b78d230d875bf5d8281112662795044680zhanyong.wan TestEventListeners& listeners = unit_test.listeners(); 124d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 125d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // Removes the default console output listener from the list so it will 126d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // not receive events from Google Test and won't print any output. Since 127d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // this operation transfers ownership of the listener to the caller we 128d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // have to delete it as well. 129d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan delete listeners.Release(listeners.default_result_printer()); 130d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 131d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // Adds the custom output listener to the list. It will now receive 132d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // events from Google Test and print the alternative output. We don't 133d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // have to worry about deleting it since Google Test assumes ownership 134d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // over it after adding it to the list. 135d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan listeners.Append(new TersePrinter); 136d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan } 137d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan int ret_val = RUN_ALL_TESTS(); 138d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 139d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // This is an example of using the UnitTest reflection API to inspect test 140d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // results. Here we discount failures from the tests we expected to fail. 141d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan int unexpectedly_failed_tests = 0; 142fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan for (int i = 0; i < unit_test.total_test_case_count(); ++i) { 143fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan const TestCase& test_case = *unit_test.GetTestCase(i); 144fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan for (int j = 0; j < test_case.total_test_count(); ++j) { 145fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan const TestInfo& test_info = *test_case.GetTestInfo(j); 146d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // Counts failed tests that were not meant to fail (those without 147d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // 'Fails' in the name). 148fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan if (test_info.result()->Failed() && 149fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan strcmp(test_info.name(), "Fails") != 0) { 150d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan unexpectedly_failed_tests++; 151d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan } 152d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan } 153d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan } 154d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 155d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan // Test that were meant to fail should not affect the test program outcome. 156d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan if (unexpectedly_failed_tests == 0) 157d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan ret_val = 0; 158d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan 159d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan return ret_val; 160d541f0259d2f6857df769f581363f84e8c4b2899zhanyong.wan} 161