提交 5cfb159e 编写于 作者: L Lucas Meneghel Rodrigues 提交者: Lucas Meneghel Rodrigues

Merge pull request #27 from ruda/test_result_junit

plugins.xunit: Add class XmlResult.
...@@ -14,10 +14,85 @@ ...@@ -14,10 +14,85 @@
"""xUnit module.""" """xUnit module."""
import sys
import datetime
from avocado.plugins import plugin from avocado.plugins import plugin
from avocado.result import TestResult from avocado.result import TestResult
class XmlResult(object):
"""
Handles the XML details for xUnit output.
"""
def __init__(self):
self.xml = ['<?xml version="1.0" encoding="UTF-8"?>']
def _escape_cdata(self, cdata):
return cdata.replace(']]>', ']]>]]&gt;<![CDATA[')
def save(self, filename):
xml = '\n'.join(self.xml)
if filename == '-':
sys.stdout.write(xml)
else:
with open(filename, 'w') as fresult:
fresult.write(xml)
def start_testsuite(self, timestamp):
self.testsuite = '<testsuite name="avocado" tests="{tests}" errors="{errors}" failures="{failures}" skip="{skip}" time="{total_time}" timestamp="%s">' % timestamp
self.testcases = []
def end_testsuite(self, tests, errors, failures, skip, total_time):
values = {'tests': tests,
'errors': errors,
'failures': failures,
'skip': skip,
'total_time': total_time}
self.xml.append(self.testsuite.format(**values))
for tc in self.testcases:
self.xml.append(tc)
self.xml.append('</testsuite>')
def add_success(self, test):
tc = '\t<testcase classname="{class}" name="{name}" time="{time}"/>'
values = {'class': test.__class__.__name__,
'name': test.tagged_name,
'time': test.time_elapsed}
self.testcases.append(tc.format(**values))
def add_skip(self, test):
tc = '''\t<testcase classname="{class}" name="{name}" time="{time}">
\t\t<skipped />
\t</testcase>'''
values = {'class': test.__class__.__name__,
'name': test.tagged_name,
'time': test.time_elapsed}
self.testcases.append(tc.format(**values))
def add_failure(self, test):
tc = '''\t<testcase classname="{class}" name="{name}" time="{time}">
\t\t<failure><![CDATA[{reason}]]></failure>
\t</testcase>'''
values = {'class': test.__class__.__name__,
'name': test.tagged_name,
'time': test.time_elapsed,
'reason': self._escape_cdata(str(test.fail_reason))}
self.testcases.append(tc.format(**values))
def add_error(self, test):
tc = '''\t<testcase classname="{class}" name="{name}" time="{time}">
\t\t<error><![CDATA[{reason}]]></error>
\t</testcase>'''
values = {'class': test.__class__.__name__,
'name': test.tagged_name,
'time': test.time_elapsed,
'reason': self._escape_cdata(str(test.fail_reason))}
self.testcases.append(tc.format(**values))
class xUnitTestResult(TestResult): class xUnitTestResult(TestResult):
""" """
...@@ -30,41 +105,36 @@ class xUnitTestResult(TestResult): ...@@ -30,41 +105,36 @@ class xUnitTestResult(TestResult):
if hasattr(self.args, 'xunit_output'): if hasattr(self.args, 'xunit_output'):
self.filename = self.args.xunit_output self.filename = self.args.xunit_output
else: else:
self.filename = 'result.xml' self.filename = '-'
self.xml = ['<?xml version="1.0" encoding="UTF-8"?>'] self.xml = XmlResult()
def start_tests(self): def start_tests(self):
TestResult.start_tests(self) TestResult.start_tests(self)
self.xml.append('<testsuite name="avocado" ' self.xml.start_testsuite(datetime.datetime.now())
'tests="{tests}" errors="{errors}" failures="{failures}" skip="{skip}">')
def start_test(self, test): def start_test(self, test):
TestResult.start_test(self, test) TestResult.start_test(self, test)
def end_test(self, test): def end_test(self, test):
TestResult.end_test(self, test) TestResult.end_test(self, test)
tc = '\t<testcase classname="{class}" name="{name}" time="{time}">' if test.status == 'PASS':
tag = test.tag self.xml.add_success(test)
if test.tag is None: if test.status == 'TEST_NA':
tag = 1 self.xml.add_skip(test)
nametag = '%s.%s' % (test.name, tag) if test.status == 'FAIL':
values = {'class': test.__class__.__name__, self.xml.add_failure(test)
'name': nametag, if test.status == 'ERROR':
'time': test.time_elapsed} self.xml.add_error(test)
self.xml.append(tc.format(**values))
self.xml.append('\t</testcase>')
def end_tests(self): def end_tests(self):
TestResult.end_tests(self) TestResult.end_tests(self)
self.xml.append('</testsuite>')
xml = '\n'.join(self.xml)
values = {'tests': self.tests_total, values = {'tests': self.tests_total,
'errors': len(self.errors), 'errors': len(self.errors),
'failures': len(self.failed), 'failures': len(self.failed),
'skip': len(self.skipped), } 'skip': len(self.skipped),
xml = xml.format(**values) 'total_time': self.total_time}
with open(self.filename, 'w') as fresult: self.xml.end_testsuite(**values)
fresult.write(xml) self.xml.save(self.filename)
class XUnit(plugin.Plugin): class XUnit(plugin.Plugin):
...@@ -80,7 +150,7 @@ class XUnit(plugin.Plugin): ...@@ -80,7 +150,7 @@ class XUnit(plugin.Plugin):
self.parser = app_parser self.parser = app_parser
app_parser.add_argument('--xunit', action='store_true') app_parser.add_argument('--xunit', action='store_true')
app_parser.add_argument('--xunit-output', app_parser.add_argument('--xunit-output',
default='result.xml', type=str, default='-', type=str,
dest='xunit_output', dest='xunit_output',
help='the file where the result should be written') help='the file where the result should be written')
self.configured = True self.configured = True
......
...@@ -26,9 +26,9 @@ class failtest(test.Test): ...@@ -26,9 +26,9 @@ class failtest(test.Test):
Functional test for avocado. Straight up fail the test. Functional test for avocado. Straight up fail the test.
""" """
def action(self, length=1): def action(self):
""" """
Sleep for length seconds. Should fail.
""" """
raise exceptions.TestFail('This test is supposed to fail') raise exceptions.TestFail('This test is supposed to fail')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册