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