avocado: Change output plugins to have extra 'output' and 'output_option' attr

In order to be able to tell whether there's more than one
output plugin trying to use the stdout, add the 'output'
and 'output_option' attributes. This changes the current
output plugins to use and set these attributes correctly.

Fix the xunit unittest to take into account the new
attribute names.
Signed-off-by: NLucas Meneghel Rodrigues <lmr@redhat.com>
上级 b60fbc19
......@@ -86,6 +86,14 @@ class TestResultJournal(TestResult):
status))
self.journal.commit()
def set_output(self):
# Journal does not output to stdout
self.output = None
def set_output_option(self):
# Journal does not need an output option
self.output_option = None
def start_test(self, test):
# lazy init because we need the toplevel logdir for the job
if not self.journal_initialized:
......
......@@ -28,6 +28,12 @@ class JSONTestResult(TestResult):
JSON Test Result class.
"""
def set_output(self):
self.output = getattr(self.args, 'json_output', '-')
def set_output_option(self):
self.output_option = '--json'
def start_tests(self):
"""
Called once before any tests are executed.
......
......@@ -168,6 +168,12 @@ class VMTestResult(TestResult):
if self.args.vm_cleanup is True and self.vm.snapshot is not None:
self.vm.restore_snapshot()
def set_output(self):
self.output = '-'
def set_output_option(self):
self.output_option = "--vm"
def start_tests(self):
"""
Called once before any tests are executed.
......
......@@ -28,8 +28,9 @@ class XmlResult(object):
Handles the XML details for xUnit output.
"""
def __init__(self):
def __init__(self, output):
self.xml = ['<?xml version="1.0" encoding="UTF-8"?>']
self.output = output
def _escape_attr(self, attrib):
return quoteattr(attrib)
......@@ -37,12 +38,14 @@ class XmlResult(object):
def _escape_cdata(self, cdata):
return cdata.replace(']]>', ']]>]]&gt;<![CDATA[')
def save(self, filename):
def save(self, filename=None):
"""
Save the XML document to a file or standard output.
:param filename: File name to save. Use '-' for standard output.
"""
if filename is None:
filename = self.output
xml = '\n'.join(self.xml)
if filename == '-':
sys.stdout.write(xml)
......@@ -158,8 +161,13 @@ class xUnitTestResult(TestResult):
:param args: an instance of :class:`argparse.Namespace`.
"""
TestResult.__init__(self, stream, args)
self.filename = getattr(self.args, 'xunit_output', '-')
self.xml = XmlResult()
self.xml = XmlResult(self.output)
def set_output(self):
self.output = getattr(self.args, 'xunit_output', '-')
def set_output_option(self):
self.output_option = '--xunit'
def start_tests(self):
"""
......@@ -199,7 +207,7 @@ class xUnitTestResult(TestResult):
'skip': len(self.skipped),
'total_time': self.total_time}
self.xml.end_testsuite(**values)
self.xml.save(self.filename)
self.xml.save()
class XUnit(plugin.Plugin):
......
......@@ -108,6 +108,33 @@ class TestResult(object):
self.failed = []
self.skipped = []
self.warned = []
# The convention is that a dash denotes stdout.
self.output = '-'
self.set_output()
self.output_option = None
self.set_output_option()
def set_output(self):
"""
Set the value of the output attribute.
By default, output is the stream (stdout), denoted by '-'.
Must be implemented by plugins, so avocado knows where the plugin wants
to output to, avoiding clashes among different plugins that want to
use the stream at the same time.
"""
pass
def set_output_option(self):
"""
Set the value of the output option (command line).
Must be implemented by plugins, so avocado prints a friendly
message to users who are using more than one plugin to print results
to stdout.
"""
pass
def start_tests(self):
"""
......
......@@ -14,6 +14,7 @@
# Copyright: Red Hat Inc. 2014
# Author: Ruda Moura <rmoura@redhat.com>
import argparse
import unittest
import os
import sys
......@@ -30,12 +31,17 @@ from avocado.plugins import xunit
from avocado import test
class ParseXMLError(Exception):
pass
class xUnitSucceedTest(unittest.TestCase):
def setUp(self):
self.tmpfile = mkstemp()
self.test_result = xunit.xUnitTestResult()
self.test_result.filename = self.tmpfile[1]
args = argparse.Namespace()
args.xunit_output = self.tmpfile[1]
self.test_result = xunit.xUnitTestResult(args=args)
self.test_result.start_tests()
self.test1 = test.Test()
self.test1.status = 'PASS'
......@@ -50,9 +56,12 @@ class xUnitSucceedTest(unittest.TestCase):
self.test_result.end_test(self.test1)
self.test_result.end_tests()
self.assertTrue(self.test_result.xml)
with open(self.test_result.filename) as fp:
with open(self.test_result.output) as fp:
xml = fp.read()
dom = minidom.parseString(xml)
try:
dom = minidom.parseString(xml)
except Exception, details:
raise ParseXMLError("Error parsing XML: '%s'.\nXML Contents:\n%s" % (details, xml))
self.assertTrue(dom)
els = dom.getElementsByTagName('testcase')
self.assertEqual(len(els), 1)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册