| 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 |
| 38 void XmlUnitTestResultPrinter::OnTestCaseStart( | 46 void XmlUnitTestResultPrinter::OnTestCaseStart( |
| 39 const testing::TestCase& test_case) { | 47 const testing::TestCase& test_case) { |
| 40 fprintf(output_file_, " <testsuite>\n"); | 48 fprintf(output_file_, " <testsuite>\n"); |
| 41 fflush(output_file_); | 49 fflush(output_file_); |
| 42 } | 50 } |
| 43 | 51 |
| 44 void XmlUnitTestResultPrinter::OnTestStart( | 52 void XmlUnitTestResultPrinter::OnTestStart( |
| 45 const testing::TestInfo& test_info) { | 53 const testing::TestInfo& test_info) { |
| 46 // This is our custom extension - it helps to recognize which test was | 54 // This is our custom extension - it helps to recognize which test was |
| 47 // running when the test binary crashed. Note that we cannot even open the | 55 // running when the test binary crashed. Note that we cannot even open the |
| (...skipping 11 matching lines...) Expand all Loading... |
| 59 " classname=\"%s\">\n", | 67 " classname=\"%s\">\n", |
| 60 test_info.name(), | 68 test_info.name(), |
| 61 static_cast<double>(test_info.result()->elapsed_time()) / | 69 static_cast<double>(test_info.result()->elapsed_time()) / |
| 62 Time::kMillisecondsPerSecond, | 70 Time::kMillisecondsPerSecond, |
| 63 test_info.test_case_name()); | 71 test_info.test_case_name()); |
| 64 if (test_info.result()->Failed()) { | 72 if (test_info.result()->Failed()) { |
| 65 fprintf(output_file_, | 73 fprintf(output_file_, |
| 66 " <failure message=\"\" type=\"\"></failure>\n"); | 74 " <failure message=\"\" type=\"\"></failure>\n"); |
| 67 } | 75 } |
| 68 for (int i = 0; i < test_info.result()->total_part_count(); ++i) { | 76 for (int i = 0; i < test_info.result()->total_part_count(); ++i) { |
| 69 WriteTestPartResult(test_info.result()->GetTestPartResult(i)); | 77 const auto& test_part_result = 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()); |
| 70 } | 81 } |
| 71 fprintf(output_file_, " </testcase>\n"); | 82 fprintf(output_file_, " </testcase>\n"); |
| 72 fflush(output_file_); | 83 fflush(output_file_); |
| 73 } | 84 } |
| 74 | 85 |
| 75 void XmlUnitTestResultPrinter::OnTestCaseEnd( | 86 void XmlUnitTestResultPrinter::OnTestCaseEnd( |
| 76 const testing::TestCase& test_case) { | 87 const testing::TestCase& test_case) { |
| 77 fprintf(output_file_, " </testsuite>\n"); | 88 fprintf(output_file_, " </testsuite>\n"); |
| 78 fflush(output_file_); | 89 fflush(output_file_); |
| 79 } | 90 } |
| 80 | 91 |
| 81 void XmlUnitTestResultPrinter::WriteTestPartResult( | 92 void XmlUnitTestResultPrinter::WriteTestPartResult( |
| 82 const testing::TestPartResult& test_part_result) { | 93 const char* file, |
| 94 int line, |
| 95 testing::TestPartResult::Type result_type, |
| 96 const std::string& summary, |
| 97 const std::string& message) { |
| 83 const char* type = "unknown"; | 98 const char* type = "unknown"; |
| 84 switch (test_part_result.type()) { | 99 switch (result_type) { |
| 85 case testing::TestPartResult::kSuccess: | 100 case testing::TestPartResult::kSuccess: |
| 86 type = "success"; | 101 type = "success"; |
| 87 break; | 102 break; |
| 88 case testing::TestPartResult::kNonFatalFailure: | 103 case testing::TestPartResult::kNonFatalFailure: |
| 89 type = "failure"; | 104 type = "failure"; |
| 90 break; | 105 break; |
| 91 case testing::TestPartResult::kFatalFailure: | 106 case testing::TestPartResult::kFatalFailure: |
| 92 type = "fatal_failure"; | 107 type = "fatal_failure"; |
| 93 break; | 108 break; |
| 94 } | 109 } |
| 95 std::string summary = test_part_result.summary(); | |
| 96 std::string summary_encoded; | 110 std::string summary_encoded; |
| 97 Base64Encode(summary, &summary_encoded); | 111 Base64Encode(summary, &summary_encoded); |
| 98 std::string message = test_part_result.message(); | |
| 99 std::string message_encoded; | 112 std::string message_encoded; |
| 100 Base64Encode(message, &message_encoded); | 113 Base64Encode(message, &message_encoded); |
| 101 fprintf(output_file_, | 114 fprintf(output_file_, |
| 102 " <x-test-result-part type=\"%s\" file=\"%s\" line=\"%d\">\n" | 115 " <x-test-result-part type=\"%s\" file=\"%s\" line=\"%d\">\n" |
| 103 " <summary>%s</summary>\n" | 116 " <summary>%s</summary>\n" |
| 104 " <message>%s</message>\n" | 117 " <message>%s</message>\n" |
| 105 " </x-test-result-part>\n", | 118 " </x-test-result-part>\n", |
| 106 type, test_part_result.file_name(), test_part_result.line_number(), | 119 type, file, line, summary_encoded.c_str(), message_encoded.c_str()); |
| 107 summary_encoded.c_str(), message_encoded.c_str()); | |
| 108 fflush(output_file_); | 120 fflush(output_file_); |
| 109 } | 121 } |
| 110 | 122 |
| 111 } // namespace base | 123 } // namespace base |
| OLD | NEW |