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.
5 #include "base/test/gtest_xml_unittest_result_printer.h"
7 #include "base/files/file_util.h"
8 #include "base/logging.h"
9 #include "base/time/time.h"
13 XmlUnitTestResultPrinter::XmlUnitTestResultPrinter() : output_file_(NULL
) {
16 XmlUnitTestResultPrinter::~XmlUnitTestResultPrinter() {
18 fprintf(output_file_
, "</testsuites>\n");
20 CloseFile(output_file_
);
24 bool XmlUnitTestResultPrinter::Initialize(const FilePath
& output_file_path
) {
25 DCHECK(!output_file_
);
26 output_file_
= OpenFile(output_file_path
, "w");
31 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testsuites>\n");
37 void XmlUnitTestResultPrinter::OnTestCaseStart(
38 const testing::TestCase
& test_case
) {
39 fprintf(output_file_
, " <testsuite>\n");
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.
49 " <x-teststart name=\"%s\" classname=\"%s\" />\n",
51 test_info
.test_case_name());
55 void XmlUnitTestResultPrinter::OnTestEnd(const testing::TestInfo
& test_info
) {
57 " <testcase name=\"%s\" status=\"run\" time=\"%.3f\""
58 " classname=\"%s\">\n",
60 static_cast<double>(test_info
.result()->elapsed_time()) /
61 Time::kMillisecondsPerSecond
,
62 test_info
.test_case_name());
63 if (test_info
.result()->Failed()) {
65 " <failure message=\"\" type=\"\"></failure>\n");
67 fprintf(output_file_
, " </testcase>\n");
71 void XmlUnitTestResultPrinter::OnTestCaseEnd(
72 const testing::TestCase
& test_case
) {
73 fprintf(output_file_
, " </testsuite>\n");