// Called after a failed assertion or a SUCCESS(). // Print the test header for the first error or // a continuation line for additional errors. // Then print the failed test summary. void TersePrinter::OnTestPartResult(const TestPartResult& test_part_result) { if (test_part_result.failed() ) { if (!test_header_printed_) { // print the message header test_header_printed_ = true; PrintTestHeader(COLOR_RED); // print the file name // the following checks for an error in a mock if (test_part_result.file_name() || test_part_result.line_number() > 0) { printf("%s:(%d)\n", test_part_result.file_name(), test_part_result.line_number()); } } else { ColoredPrintf(COLOR_RED, "%s", "[ ]\n"); } // print the failed test summary PrintFailedTestSummary(string(test_part_result.summary())); fflush(stdout); } }
int main(int argc, char** argv) { // Parse command line BEFORE testing::InitGoogleTest bool use_terse_printer = true; // true for TersePrinter, false for gtest default bool use_terse_output = false; // print terse output if --terse_output option is set bool use_color = true; for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "--terse_output") == 0 ) use_terse_output = true; else if (strcmp(argv[i], "--gtest_color=no") == 0 ) use_color = false; } // Do this after parsing the command line but before changing printer. testing::InitGoogleTest(&argc, argv); // If we are given the --terse_printer command line flag, suppresses the // standard output and attaches own result printer. if (use_terse_printer) { UnitTest& unit_test = *UnitTest::GetInstance(); testing::TestEventListeners& listeners = unit_test.listeners(); // Removes the default console output listener from the list so it will // not receive events from Google Test and won't print any output. Since // this operation transfers ownership of the listener to the caller we // have to delete it as well. delete listeners.Release(listeners.default_result_printer()); // Adds the custom output listener to the list. It will now receive // events from Google Test and print the alternative output. We don't // have to worry about deleting it since Google Test assumes ownership // over it after adding it to the list. listeners.Append(new TersePrinter(use_terse_output, use_color)); } int retval = RUN_ALL_TESTS(); if (!use_terse_printer) ColoredPrintf(COLOR_YELLOW, "\n* USING DEFAULT GTEST PRINTER *\n\n"); else { if (use_terse_output) ColoredPrintf(COLOR_YELLOW, "\n* USING TersePrinter AND --terse_output *\n\n"); else ColoredPrintf(COLOR_YELLOW, "\n* NOT USING --terse_output *\n\n"); } return retval; }
// Called before environment tear-down for each iteration of tests starts. void TersePrinter::OnEnvironmentsTearDownStart(const UnitTest& unit_test) { // need a linefeed if a test case end is not printed if ((useTerseOutput && unit_test.failed_test_count() != 0) || !GTEST_FLAG(print_time)) printf("\n"); ColoredPrintf(COLOR_GREEN, "%s", "[----------] "); printf("Global test environment tear-down.\n"); fflush(stdout); }
// Called before the test case starts. void TersePrinter::OnTestCaseStart(const TestCase& test_case) { if (useTerseOutput) return; ColoredPrintf(COLOR_GREEN, "[----------] "); printf("%s from %s\n", FormatTestCount(test_case.test_to_run_count()).c_str(), test_case.name()); fflush(stdout); }
// Called after the test case ends. void TersePrinter::OnTestCaseEnd(const TestCase& test_case) { // a test case end is not printed for the following if (useTerseOutput || !GTEST_FLAG(print_time)) return; ColoredPrintf(COLOR_GREEN, "[----------] "); printf("%s from %s (%s ms total)\n\n", FormatTestCount(test_case.test_to_run_count()).c_str(), test_case.name(), internal::StreamableToString(test_case.elapsed_time()).c_str()); fflush(stdout); }
int main(int argc, char** argv) { // parse command line BEFORE InitGoogleTest bool useTersePrinter = true; // ALWAYS true (for testing only) bool useTerseOutput = false; // option for terse (true) or all (false) bool useColor = true; bool noClose = false; for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "--terse_output") == 0 ) useTerseOutput = true; else if (strcmp(argv[i], "--no_close") == 0 ) noClose = true; else if (strcmp(argv[i], "--gtest_color=no") == 0 ) useColor = false; } // do this after parsing the command line but before changing printer testing::InitGoogleTest(&argc, argv); // change to TersePrinter if (useTersePrinter) { UnitTest& unit_test = *UnitTest::GetInstance(); TestEventListeners& listeners = unit_test.listeners(); delete listeners.Release(listeners.default_result_printer()); listeners.Append(new TersePrinter(useTerseOutput, useColor)); } // run the tests int retval = RUN_ALL_TESTS(); // Verify that all tests were run. This can occur if a source file // is missing from the project. The UnitTest reflection API in // example 9 will not work here because of user modifications. // Change the following value to the number of tests (within 20). if (useTersePrinter) TersePrinter::PrintTestTotals(260, __FILE__, __LINE__); else ColoredPrintf(COLOR_YELLOW, "\n* USING DEFAULT GTEST PRINTER *\n\n"); #ifdef __WIN32 printf("%c", '\n'); #endif if (noClose) // command line option SystemPause(); return retval; }
// Static function to print the test total. // Called from the "main" test program at end of job. void TersePrinter::PrintTestTotals(int all_test_total_check, const char* file, int line) { const char* const filter = GTEST_FLAG(filter).c_str(); // Get the file name. string file_path(file); size_t start = file_path.find_last_of("/\\"); if (start == string::npos) start = 0; else start++; string file_name = file_path.substr(start); // Check the totals. if (strcmp("*", filter) != 0) { // Prints the filter if it's not *. // This reminds the user that some tests may be skipped. ColoredPrintf(COLOR_YELLOW, "\nTest filter = %s\n", filter); } else if (g_test_to_run < all_test_total_check - 20) { // All tests ran but there are missing tests. // Test files may have been removed from the tests. ColoredPrintf(COLOR_RED, "\nMISSING TESTS: %d (%d) %s:(%d)\n", all_test_total_check, g_test_to_run, file_name.c_str(), line); } else if (g_test_to_run > all_test_total_check + 20) { // All tests ran but there too many tests. // The test variable needs to be updated. ColoredPrintf(COLOR_RED, "\nUpdate test variable: %d (%d) %s(%d)\n", all_test_total_check, g_test_to_run, file_name.c_str(), line); } else ColoredPrintf(COLOR_GREEN, "%s", "\nAll tests ran.\n"); if (GTEST_FLAG(shuffle)) ColoredPrintf(COLOR_YELLOW, "Randomized test order.\n"); #ifdef __BORLANDC__ ColoredPrintf(COLOR_YELLOW, "%s", "No mock tests.\n"); #endif #if !GTEST_HAS_DEATH_TEST || LEAK_DETECTOR || LEAK_FINDER ColoredPrintf(COLOR_YELLOW, "%s", "No death tests.\n"); #endif }
// Internal helper for printing the list of failed tests at end of run. void TersePrinter::PrintFailedTestsList(const UnitTest& unit_test) const { const int failed_test_count = unit_test.failed_test_count(); if (failed_test_count == 0) return; for (int i = 0; i < unit_test.total_test_case_count(); ++i) { const TestCase& test_case = *unit_test.GetTestCase(i); if (!test_case.should_run() || (test_case.failed_test_count() == 0)) continue; for (int j = 0; j < test_case.total_test_count(); ++j) { const TestInfo& test_info = *test_case.GetTestInfo(j); if (!test_info.should_run() || test_info.result()->Passed()) continue; ColoredPrintf(COLOR_RED, "%s", "[ FAILED ] "); printf("%s.%s\n", test_case.name(), test_info.name()); } } }
int main(int argc, char** argv) { // set global variable g_testDirectory setTestDirectory(); // the following statement will be printed at beginning of job // and before a death test // printf("Test directory: %s.\n", (*g_testDirectory).c_str()); // parse command line BEFORE InitGoogleTest bool useTersePrinter = true; // ALWAYS true (for testing only) bool useTerseOutput = false; // option for terse (true) or all (false) bool useColor = true; bool noClose = false; for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "--terse_output") == 0 ) useTerseOutput = true; else if (strcmp(argv[i], "--no_close") == 0 ) noClose = true; else if (strcmp(argv[i], "--gtest_color=no") == 0 ) useColor = false; } // do this after parsing the command line but before changing printer testing::InitGoogleTest(&argc, argv); // change to TersePrinter if (useTersePrinter) { UnitTest& unit_test = *UnitTest::GetInstance(); testing::TestEventListeners& listeners = unit_test.listeners(); delete listeners.Release(listeners.default_result_printer()); listeners.Append(new TersePrinter(useTerseOutput, useColor)); } // begin unit testing createTestDirectory(getTestDirectory()); int retval = RUN_ALL_TESTS(); // Print verification if terse_printer. // Verify that all tests were run. This can occur if a source file // is missing from the project. The UnitTest reflection API in // example 9 will not work here because of user modifications. if (useTersePrinter) { if (g_isI18nTest) // Change the following value to the number of tests (within 10). TersePrinter::PrintTestTotals( 70 , __FILE__, __LINE__ ); else // Change the following value to the number of tests (within 10). TersePrinter::PrintTestTotals( 97 , __FILE__, __LINE__); } else ColoredPrintf(COLOR_YELLOW, "\n* USING DEFAULT GTEST PRINTER *\n\n"); if (g_isI18nTest) printI18nMessage(); #ifdef __WIN32 printf("%c", '\n'); #endif // end of unit testing removeTestDirectory(getTestDirectory()); if (noClose) // command line option systemPause(); return retval; }
// Called before environment set-up for each iteration of tests starts. void TersePrinter::OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) { ColoredPrintf(COLOR_GREEN, "%s", "[----------] "); printf("Global test environment set-up.\n\n"); fflush(stdout); }
// Internal helper for printing the test header. void TersePrinter::PrintTestHeader(ConsoleColor color) const { // print the message header ColoredPrintf(color, "%s", "[ RUN ] "); printf("%s.%s\n", test_case_name_.c_str(), test_info_name_.c_str()); }
// Internal helper for printing the TestPartResult summary variable // with color enhancement. void TersePrinter::PrintFailedTestSummary(const string& summary_) const { // Replace "\\n" and "\\r" sequences with LF and CR (version 1.7). string summary = ReplaceSummaryString(summary_); // Separate the summary message into lines. vector<string> line; // vector for storing print lines size_t prev_i = 0; // i value of the previous \n for (size_t i = 1; i < summary.length(); i++) { // end the line when the previous char is the last \n in a sequence if (summary[i - 1] != '\n' || summary[i] == '\n') continue; line.push_back(summary.substr(prev_i, i - prev_i)); prev_i = i; } // Write the last line and append a return. if (prev_i < (summary.length())) { line.push_back(summary.substr(prev_i)); int last_char = static_cast<int>(line.back().length()) - 1; if (last_char > -1 && line.back()[last_char] != '\n') line.back().append("\n"); } // Print the lines with added color. for (size_t j = 0; j < line.size(); j++) { if (line[j].compare(0, 9, " Actual:") == 0 || line[j].compare(0, 9, "Which is:") == 0 || line[j].compare(0, 9, "Value of:") == 0) { // Header portion is not colored. printf("%s", line[j].substr(0, 10).c_str()); ColoredPrintf(COLOR_CYAN, "%s", line[j].substr(10).c_str()); } else if (line[j].compare(0, 9, "Expected:") == 0) { // Header portion is not colored. printf("%s", line[j].substr(0, 10).c_str()); size_t iPrint = 10; // Check for another header. size_t iHeading = line[j].find(", actual:"); if (iHeading != string::npos) { ColoredPrintf(COLOR_CYAN, "%s", line[j].substr(iPrint, iHeading - iPrint).c_str()); iPrint = iHeading; // Break the line at actual and reposition. line[j].replace(iHeading, 2, "\n "); // Header portion is not colored. printf("%s", line[j].substr(iHeading, 10).c_str()); iPrint += 10; } ColoredPrintf(COLOR_CYAN, "%s", line[j].substr(iPrint).c_str()); } else if (line[j].compare(0, 11, "Death test:") == 0 || line[j].compare(0, 11, " Result:") == 0 || line[j].compare(0, 11, " Expected:") == 0 || line[j].compare(0, 11, "Actual msg:") == 0 || line[j].compare(0, 11, " Error msg:") == 0) { // Header portion is not colored. printf("%s", line[j].substr(0, 12).c_str()); ColoredPrintf(COLOR_CYAN, "%s", line[j].substr(12).c_str()); } // mock call headers else if (line[j].compare(0, 29, "Unexpected mock function call") == 0 || line[j].compare(0, 31, "Google Mock tried the following") == 0 || line[j].compare(0, 40, "Actual function call count doesn't match") == 0) { // Entire is not colored. printf("%s", line[j].c_str()); } // mock call errors else if (line[j].compare(0, 18, " Function call:") == 0 || line[j].compare(0, 18, " Returns:") == 0 || line[j].compare(0, 16, " Expected arg #") == 0 // compare only 16 || line[j].compare(0, 18, " Actual:") == 0 || line[j].compare(0, 18, " Expected:") == 0) { // Header portion is not colored. printf("%s", line[j].substr(0, 18).c_str()); ColoredPrintf(COLOR_CYAN, "%s", line[j].substr(18).c_str()); } else { ColoredPrintf(COLOR_CYAN, "%s", line[j].c_str()); } } }