| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/test/gtest_xml_unittest_result_printer.h" | 5 #include "base/test/gtest_xml_unittest_result_printer.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 if (!output_file_) | 28 if (!output_file_) |
| 29 return false; | 29 return false; |
| 30 | 30 |
| 31 fprintf(output_file_, | 31 fprintf(output_file_, |
| 32 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testsuites>\n"); | 32 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testsuites>\n"); |
| 33 fflush(output_file_); | 33 fflush(output_file_); |
| 34 | 34 |
| 35 return true; | 35 return true; |
| 36 } | 36 } |
| 37 | 37 |
| 38 void XmlUnitTestResultPrinter::OnAssert(const char* file, | |
| 39 int line, | |
| 40 const std::string& summary, | |
| 41 const std::string& message) { | |
| 42 WriteTestPartResult(file, line, testing::TestPartResult::kFatalFailure, | |
| 43 summary, message); | |
| 44 } | |
| 45 | |
| 46 void XmlUnitTestResultPrinter::OnTestCaseStart( | 38 void XmlUnitTestResultPrinter::OnTestCaseStart( |
| 47 const testing::TestCase& test_case) { | 39 const testing::TestCase& test_case) { |
| 48 fprintf(output_file_, " <testsuite>\n"); | 40 fprintf(output_file_, " <testsuite>\n"); |
| 49 fflush(output_file_); | 41 fflush(output_file_); |
| 50 } | 42 } |
| 51 | 43 |
| 52 void XmlUnitTestResultPrinter::OnTestStart( | 44 void XmlUnitTestResultPrinter::OnTestStart( |
| 53 const testing::TestInfo& test_info) { | 45 const testing::TestInfo& test_info) { |
| 54 // This is our custom extension - it helps to recognize which test was | 46 // This is our custom extension - it helps to recognize which test was |
| 55 // running when the test binary crashed. Note that we cannot even open the | 47 // running when the test binary crashed. Note that we cannot even open the |
| (...skipping 11 matching lines...) Expand all Loading... |
| 67 " classname=\"%s\">\n", | 59 " classname=\"%s\">\n", |
| 68 test_info.name(), | 60 test_info.name(), |
| 69 static_cast<double>(test_info.result()->elapsed_time()) / | 61 static_cast<double>(test_info.result()->elapsed_time()) / |
| 70 Time::kMillisecondsPerSecond, | 62 Time::kMillisecondsPerSecond, |
| 71 test_info.test_case_name()); | 63 test_info.test_case_name()); |
| 72 if (test_info.result()->Failed()) { | 64 if (test_info.result()->Failed()) { |
| 73 fprintf(output_file_, | 65 fprintf(output_file_, |
| 74 " <failure message=\"\" type=\"\"></failure>\n"); | 66 " <failure message=\"\" type=\"\"></failure>\n"); |
| 75 } | 67 } |
| 76 for (int i = 0; i < test_info.result()->total_part_count(); ++i) { | 68 for (int i = 0; i < test_info.result()->total_part_count(); ++i) { |
| 77 const auto& test_part_result = test_info.result()->GetTestPartResult(i); | 69 WriteTestPartResult(test_info.result()->GetTestPartResult(i)); |
| 78 WriteTestPartResult(test_part_result.file_name(), | |
| 79 test_part_result.line_number(), test_part_result.type(), | |
| 80 test_part_result.summary(), test_part_result.message()); | |
| 81 } | 70 } |
| 82 fprintf(output_file_, " </testcase>\n"); | 71 fprintf(output_file_, " </testcase>\n"); |
| 83 fflush(output_file_); | 72 fflush(output_file_); |
| 84 } | 73 } |
| 85 | 74 |
| 86 void XmlUnitTestResultPrinter::OnTestCaseEnd( | 75 void XmlUnitTestResultPrinter::OnTestCaseEnd( |
| 87 const testing::TestCase& test_case) { | 76 const testing::TestCase& test_case) { |
| 88 fprintf(output_file_, " </testsuite>\n"); | 77 fprintf(output_file_, " </testsuite>\n"); |
| 89 fflush(output_file_); | 78 fflush(output_file_); |
| 90 } | 79 } |
| 91 | 80 |
| 92 void XmlUnitTestResultPrinter::WriteTestPartResult( | 81 void XmlUnitTestResultPrinter::WriteTestPartResult( |
| 93 const char* file, | 82 const testing::TestPartResult& test_part_result) { |
| 94 int line, | |
| 95 testing::TestPartResult::Type result_type, | |
| 96 const std::string& summary, | |
| 97 const std::string& message) { | |
| 98 const char* type = "unknown"; | 83 const char* type = "unknown"; |
| 99 switch (result_type) { | 84 switch (test_part_result.type()) { |
| 100 case testing::TestPartResult::kSuccess: | 85 case testing::TestPartResult::kSuccess: |
| 101 type = "success"; | 86 type = "success"; |
| 102 break; | 87 break; |
| 103 case testing::TestPartResult::kNonFatalFailure: | 88 case testing::TestPartResult::kNonFatalFailure: |
| 104 type = "failure"; | 89 type = "failure"; |
| 105 break; | 90 break; |
| 106 case testing::TestPartResult::kFatalFailure: | 91 case testing::TestPartResult::kFatalFailure: |
| 107 type = "fatal_failure"; | 92 type = "fatal_failure"; |
| 108 break; | 93 break; |
| 109 } | 94 } |
| 95 std::string summary = test_part_result.summary(); |
| 110 std::string summary_encoded; | 96 std::string summary_encoded; |
| 111 Base64Encode(summary, &summary_encoded); | 97 Base64Encode(summary, &summary_encoded); |
| 98 std::string message = test_part_result.message(); |
| 112 std::string message_encoded; | 99 std::string message_encoded; |
| 113 Base64Encode(message, &message_encoded); | 100 Base64Encode(message, &message_encoded); |
| 114 fprintf(output_file_, | 101 fprintf(output_file_, |
| 115 " <x-test-result-part type=\"%s\" file=\"%s\" line=\"%d\">\n" | 102 " <x-test-result-part type=\"%s\" file=\"%s\" line=\"%d\">\n" |
| 116 " <summary>%s</summary>\n" | 103 " <summary>%s</summary>\n" |
| 117 " <message>%s</message>\n" | 104 " <message>%s</message>\n" |
| 118 " </x-test-result-part>\n", | 105 " </x-test-result-part>\n", |
| 119 type, file, line, summary_encoded.c_str(), message_encoded.c_str()); | 106 type, test_part_result.file_name(), test_part_result.line_number(), |
| 107 summary_encoded.c_str(), message_encoded.c_str()); |
| 120 fflush(output_file_); | 108 fflush(output_file_); |
| 121 } | 109 } |
| 122 | 110 |
| 123 } // namespace base | 111 } // namespace base |
| OLD | NEW |