提交 10fb8f69 编写于 作者: L Lukáš Doktor 提交者: Cleber Rosa

avocado.core.result: Supply only "job" (and output for json/xunit/...)

Both "stream" and "args" are present in the job. Let's only pass
the "job" object and let the Result class to take the needed objects.

Additionally allow overriding the output for json/xunit/html in order
to allow users to specify different value than the one from args.
Signed-off-by: NLukáš Doktor <ldoktor@redhat.com>
上级 682b1d78
......@@ -24,7 +24,6 @@ import urllib
import pystache
from . import output
from .result import TestResult
from ..utils import path as utils_path
from ..utils import runtime
......@@ -189,11 +188,16 @@ class HTMLTestResult(TestResult):
command_line_arg_name = '--html'
def __init__(self, stream=None, args=None):
TestResult.__init__(self, stream, args)
self.output = getattr(self.args, 'html_output')
self.args = args
self.view = output.View(app_args=args)
def __init__(self, job, force_html_file=None):
"""
:param job: Job which defines this result
:param force_html_file: Override the output html file location
"""
TestResult.__init__(self, job)
if force_html_file:
self.output = force_html_file
else:
self.output = self.args.html_output
self.json = None
def start_tests(self):
......@@ -288,7 +292,7 @@ class HTMLTestResult(TestResult):
report_file.write(report_contents)
if self.args is not None:
if getattr(self.args, 'open_browser'):
if getattr(self.args, 'open_browser', False):
# if possible, put browser in separate process group, so
# keyboard interrupts don't affect browser as well as Python
setsid = getattr(os, 'setsid', None)
......
......@@ -195,7 +195,7 @@ class Job(object):
def _set_output_plugins(self):
if getattr(self.args, 'test_result_classes', None) is not None:
for klass in self.args.test_result_classes:
test_result_instance = klass(self.view, self.args)
test_result_instance = klass(self)
self.result_proxy.add_output_plugin(test_result_instance)
def _make_test_result(self):
......@@ -216,26 +216,18 @@ class Job(object):
# Setup the xunit plugin to output to the debug directory
xunit_file = os.path.join(self.logdir, 'results.xml')
args = argparse.Namespace()
args.xunit_output = xunit_file
xunit_plugin = xunit.xUnitTestResult(self.view, args)
xunit_plugin = xunit.xUnitTestResult(self, xunit_file)
self.result_proxy.add_output_plugin(xunit_plugin)
# Setup the json plugin to output to the debug directory
json_file = os.path.join(self.logdir, 'results.json')
args = argparse.Namespace()
args.json_output = json_file
json_plugin = jsonresult.JSONTestResult(self.view, args)
json_plugin = jsonresult.JSONTestResult(self, json_file)
self.result_proxy.add_output_plugin(json_plugin)
# Setup the html output to the results directory
if HTML_REPORT_SUPPORT:
html_file = os.path.join(self.logdir, 'html', 'results.html')
args = argparse.Namespace()
args.html_output = html_file
args.open_browser = getattr(self.args, 'open_browser', False)
args.relative_links = True
html_plugin = html.HTMLTestResult(self.view, args)
html_plugin = html.HTMLTestResult(self, html_file)
self.result_proxy.add_output_plugin(html_plugin)
op_set_stdout = self.result_proxy.output_plugins_using_stdout()
......@@ -249,7 +241,7 @@ class Job(object):
sys.exit(exit_codes.AVOCADO_JOB_FAIL)
if not op_set_stdout and not self.standalone:
human_plugin = result.HumanTestResult(self.view, self.args)
human_plugin = result.HumanTestResult(self)
self.result_proxy.add_output_plugin(human_plugin)
def _make_test_suite(self, urls=None):
......
......@@ -30,10 +30,17 @@ class JSONTestResult(TestResult):
command_line_arg_name = '--json'
def __init__(self, stream=None, args=None):
TestResult.__init__(self, stream, args)
self.output = getattr(self.args, 'json_output', '-')
self.view = output.View(app_args=args)
def __init__(self, job, force_json_file=None):
"""
:param job: Job which defines this result
:param force_html_file: Override the json output file location
"""
TestResult.__init__(self, job)
if force_json_file:
self.output = force_json_file
else:
self.output = getattr(self.args, 'json_output', '-')
self.view = output.View(app_args=self.args)
def start_tests(self):
"""
......@@ -67,7 +74,7 @@ class JSONTestResult(TestResult):
self.json['tests'].append(t)
def _save_json(self):
with open(self.args.json_output, 'w') as j:
with open(self.output, 'w') as j:
j.write(self.json)
def end_tests(self):
......@@ -84,7 +91,7 @@ class JSONTestResult(TestResult):
'time': self.total_time
})
self.json = json.dumps(self.json)
if self.args.json_output == '-':
if self.output == '-':
self.view.notify(event='minor', msg=self.json)
else:
self._save_json()
......@@ -27,14 +27,13 @@ class RemoteTestResult(HumanTestResult):
command_line_arg_name = '--remote-hostname'
def __init__(self, stream, args):
def __init__(self, job):
"""
Creates an instance of RemoteTestResult.
:param stream: an instance of :class:`avocado.core.output.View`.
:param args: an instance of :class:`argparse.Namespace`.
:param job: an instance of :class:`avocado.core.job.Job`.
"""
HumanTestResult.__init__(self, stream, args)
HumanTestResult.__init__(self, job)
self.test_dir = os.getcwd()
self.remote_test_dir = '~/avocado/tests'
self.urls = self.args.url
......@@ -54,6 +53,6 @@ class VMTestResult(RemoteTestResult):
command_line_arg_name = '--vm-domain'
def __init__(self, stream, args):
super(VMTestResult, self).__init__(stream, args)
def __init__(self, job):
super(VMTestResult, self).__init__(job)
self.vm = None
......@@ -118,16 +118,15 @@ class TestResult(object):
#: inconsistencies come from.
command_line_arg_name = None
def __init__(self, stream, args):
def __init__(self, job):
"""
Creates an instance of TestResult.
:param stream: an instance of :class:`avocado.core.output.View`.
:param args: an instance of :class:`argparse.Namespace`.
:param job: an instance of :class:`avocado.core.job.Job`.
"""
self.stream = stream
self.args = args
self.tests_total = getattr(args, 'test_result_total', 1)
self.args = getattr(job, "args", None)
self.stream = getattr(job, "view", None)
self.tests_total = getattr(self.args, 'test_result_total', 1)
self.tests_run = 0
self.total_time = 0.0
self.passed = []
......
......@@ -154,16 +154,19 @@ class xUnitTestResult(TestResult):
command_line_arg_name = '--xunit'
def __init__(self, stream=None, args=None):
def __init__(self, job, force_xunit_file=None):
"""
Creates an instance of xUnitTestResult.
:param stream: an instance of :class:`avocado.core.output.View`.
:param args: an instance of :class:`argparse.Namespace`.
:param job: an instance of :class:`avocado.core.job.Job`.
:param force_xunit_file: Override the output file defined in job.args
"""
TestResult.__init__(self, stream, args)
self.output = getattr(self.args, 'xunit_output', '-')
self.stream = output.View(app_args=args)
TestResult.__init__(self, job)
if force_xunit_file:
self.output = force_xunit_file
else:
self.output = getattr(self.args, 'xunit_output', '-')
self.stream = output.View(app_args=self.args)
self.xml = XmlResult()
def start_tests(self):
......
......@@ -45,14 +45,13 @@ class TestResultJournal(TestResult):
command_line_arg_name = '--journal'
def __init__(self, stream=None, args=None):
def __init__(self, job=None):
"""
Creates an instance of TestResultJournal.
:param stream: an instance of :class:`avocado.core.output.View`.
:param args: an instance of :class:`argparse.Namespace`.
:param job: an instance of :class:`avocado.core.job.Job`.
"""
TestResult.__init__(self, stream, args)
TestResult.__init__(self, job)
self.journal_initialized = False
def _init_journal(self, logdir):
......
from flexmock import flexmock
import unittest
import os
import json
......@@ -45,7 +46,8 @@ class JSONResultTest(unittest.TestCase):
args = argparse.Namespace(json_output=self.tmpfile[1])
stream = _Stream()
stream.logfile = 'debug.log'
self.test_result = jsonresult.JSONTestResult(stream, args)
dummyjob = flexmock(view=stream, args=args)
self.test_result = jsonresult.JSONTestResult(dummyjob)
self.test_result.filename = self.tmpfile[1]
self.test_result.start_tests()
self.test1 = SimpleTest(job=job.Job(), base_logdir=self.tmpdir)
......
from flexmock import flexmock
import argparse
import unittest
import os
......@@ -48,7 +49,8 @@ class xUnitSucceedTest(unittest.TestCase):
self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__)
args = argparse.Namespace()
args.xunit_output = self.tmpfile[1]
self.test_result = xunit.xUnitTestResult(stream=_Stream(), args=args)
dummy_job = flexmock(view=_Stream(), args=args)
self.test_result = xunit.xUnitTestResult(dummy_job)
self.test_result.start_tests()
self.test1 = SimpleTest(job=job.Job(), base_logdir=self.tmpdir)
self.test1.status = 'PASS'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册