OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/test/gtest_xml_unittest_result_printer.h" |
| 6 |
| 7 #include "base/files/file_util.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/time/time.h" |
| 10 |
| 11 namespace base { |
| 12 |
| 13 XmlUnitTestResultPrinter::XmlUnitTestResultPrinter() : output_file_(NULL) { |
| 14 } |
| 15 |
| 16 XmlUnitTestResultPrinter::~XmlUnitTestResultPrinter() { |
| 17 if (output_file_) { |
| 18 fprintf(output_file_, "</testsuites>\n"); |
| 19 fflush(output_file_); |
| 20 CloseFile(output_file_); |
| 21 } |
| 22 } |
| 23 |
| 24 bool XmlUnitTestResultPrinter::Initialize(const FilePath& output_file_path) { |
| 25 DCHECK(!output_file_); |
| 26 output_file_ = OpenFile(output_file_path, "w"); |
| 27 if (!output_file_) |
| 28 return false; |
| 29 |
| 30 fprintf(output_file_, |
| 31 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testsuites>\n"); |
| 32 fflush(output_file_); |
| 33 |
| 34 return true; |
| 35 } |
| 36 |
| 37 void XmlUnitTestResultPrinter::OnTestCaseStart( |
| 38 const testing::TestCase& test_case) { |
| 39 fprintf(output_file_, " <testsuite>\n"); |
| 40 fflush(output_file_); |
| 41 } |
| 42 |
| 43 void XmlUnitTestResultPrinter::OnTestStart( |
| 44 const testing::TestInfo& test_info) { |
| 45 // This is our custom extension - it helps to recognize which test was |
| 46 // running when the test binary crashed. Note that we cannot even open the |
| 47 // <testcase> tag here - it requires e.g. run time of the test to be known. |
| 48 fprintf(output_file_, |
| 49 " <x-teststart name=\"%s\" classname=\"%s\" />\n", |
| 50 test_info.name(), |
| 51 test_info.test_case_name()); |
| 52 fflush(output_file_); |
| 53 } |
| 54 |
| 55 void XmlUnitTestResultPrinter::OnTestEnd(const testing::TestInfo& test_info) { |
| 56 fprintf(output_file_, |
| 57 " <testcase name=\"%s\" status=\"run\" time=\"%.3f\"" |
| 58 " classname=\"%s\">\n", |
| 59 test_info.name(), |
| 60 static_cast<double>(test_info.result()->elapsed_time()) / |
| 61 Time::kMillisecondsPerSecond, |
| 62 test_info.test_case_name()); |
| 63 if (test_info.result()->Failed()) { |
| 64 fprintf(output_file_, |
| 65 " <failure message=\"\" type=\"\"></failure>\n"); |
| 66 } |
| 67 fprintf(output_file_, " </testcase>\n"); |
| 68 fflush(output_file_); |
| 69 } |
| 70 |
| 71 void XmlUnitTestResultPrinter::OnTestCaseEnd( |
| 72 const testing::TestCase& test_case) { |
| 73 fprintf(output_file_, " </testsuite>\n"); |
| 74 fflush(output_file_); |
| 75 } |
| 76 |
| 77 } // namespace base |
OLD | NEW |