Source code for robot.reporting.xunitwriter

#  Copyright 2008-2015 Nokia Networks
#  Copyright 2016-     Robot Framework Foundation
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  See the License for the specific language governing permissions and
#  limitations under the License.

from robot.result import ResultVisitor
from robot.utils import XmlWriter

[docs]class XUnitWriter: def __init__(self, execution_result): self._execution_result = execution_result
[docs] def write(self, output): xml_writer = XmlWriter(output, usage='xunit') writer = XUnitFileWriter(xml_writer) self._execution_result.visit(writer)
[docs]class XUnitFileWriter(ResultVisitor): """Provides an xUnit-compatible result file. Attempts to adhere to the de facto schema guessed by Peter Reilly, see: """ def __init__(self, xml_writer): self._writer = xml_writer
[docs] def start_suite(self, suite): tests, failures, skipped = self._get_stats(suite.statistics) attrs = {'name':, 'tests': tests, 'errors': '0', 'failures': failures, 'skipped': skipped, 'time': self._time_as_seconds(suite.elapsedtime), 'timestamp' : self._starttime_to_isoformat(suite.starttime)} self._writer.start('testsuite', attrs)
def _get_stats(self, statistics): return ( str(, str(statistics.failed), str(statistics.skipped) )
[docs] def end_suite(self, suite): if suite.metadata or suite.doc: self._writer.start('properties') if suite.doc: self._writer.element('property', attrs={'name': 'Documentation', 'value': suite.doc}) for meta_name, meta_value in suite.metadata.items(): self._writer.element('property', attrs={'name': meta_name, 'value': meta_value}) self._writer.end('properties') self._writer.end('testsuite')
[docs] def visit_test(self, test): self._writer.start('testcase', {'classname': test.parent.longname, 'name':, 'time': self._time_as_seconds(test.elapsedtime)}) if test.failed: self._writer.element('failure', attrs={'message': test.message, 'type': 'AssertionError'}) if test.skipped: self._writer.element('skipped', attrs={'message': test.message, 'type': 'SkipExecution'}) self._writer.end('testcase')
def _time_as_seconds(self, millis): return '{:.3f}'.format(millis / 1000)
[docs] def visit_keyword(self, kw): pass
[docs] def visit_statistics(self, stats): pass
[docs] def visit_errors(self, errors): pass
[docs] def end_result(self, result): self._writer.close()
def _starttime_to_isoformat(self, stime): if not stime: return None return f'{stime[:4]}-{stime[4:6]}-{stime[6:8]}T{stime[9:22]}000'