Robot Framework Integrated Development Environment (RIDE)
xunitwriter.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from __future__ import division
17 
18 from robotide.lib.robot.result import ResultVisitor
19 from robotide.lib.robot.utils import XmlWriter
20 
21 
22 class XUnitWriter():
23 
24  def __init__(self, execution_result, skip_noncritical):
25  self._execution_result_execution_result = execution_result
26  self._skip_noncritical_skip_noncritical = skip_noncritical
27 
28  def write(self, output):
29  writer = XUnitFileWriter(XmlWriter(output), self._skip_noncritical_skip_noncritical)
30  self._execution_result_execution_result.visit(writer)
31 
32 
33 
38 class XUnitFileWriter(ResultVisitor):
39 
40  def __init__(self, xml_writer, skip_noncritical=False):
41  self._writer_writer = xml_writer
42  self._root_suite_root_suite = None
43  self._skip_noncritical_skip_noncritical = skip_noncritical
44 
45  def start_suite(self, suite):
46  if self._root_suite_root_suite:
47  return
48  self._root_suite_root_suite = suite
49  tests, failures, skipped = self._get_stats_get_stats(suite.statistics)
50  attrs = {'name': suite.name,
51  'tests': tests,
52  'errors': '0',
53  'failures': failures,
54  'skipped': skipped,
55  'time': self._time_as_seconds_time_as_seconds(suite.elapsedtime)}
56  self._writer_writer.start('testsuite', attrs)
57 
58  def _get_stats(self, statistics):
59  if self._skip_noncritical_skip_noncritical:
60  failures = statistics.critical.failed
61  skipped = statistics.all.total - statistics.critical.total
62  else:
63  failures = statistics.all.failed
64  skipped = 0
65  return str(statistics.all.total), str(failures), str(skipped)
66 
67  def end_suite(self, suite):
68  if suite is self._root_suite_root_suite:
69  self._writer_writer.end('testsuite')
70 
71  def visit_test(self, test):
72  self._writer_writer.start('testcase',
73  {'classname': test.parent.longname,
74  'name': test.name,
75  'time': self._time_as_seconds_time_as_seconds(test.elapsedtime)})
76  if self._skip_noncritical_skip_noncritical and not test.critical:
77  self._skip_test_skip_test(test)
78  elif not test.passed:
79  self._fail_test_fail_test(test)
80  self._writer_writer.end('testcase')
81 
82  def _skip_test(self, test):
83  self._writer_writer.element('skipped', '%s: %s' % (test.status, test.message)
84  if test.message else test.status)
85 
86  def _fail_test(self, test):
87  self._writer_writer.element('failure', attrs={'message': test.message,
88  'type': 'AssertionError'})
89 
90  def _time_as_seconds(self, millis):
91  return '{:.3f}'.format(millis / 1000)
92 
93  def visit_keyword(self, kw):
94  pass
95 
96  def visit_statistics(self, stats):
97  pass
98 
99  def visit_errors(self, errors):
100  pass
101 
102  def end_result(self, result):
103  self._writer.close()
Provides an xUnit-compatible result file.
Definition: xunitwriter.py:38
def __init__(self, xml_writer, skip_noncritical=False)
Definition: xunitwriter.py:40
def __init__(self, execution_result, skip_noncritical)
Definition: xunitwriter.py:24