提交 b3e59bcd 编写于 作者: R Rudá Moura 提交者: Ruda Moura

Remove complexity when creating test results and jobs.

- Change TestResult constructor signature to accept two parameters:
stream and args, everything else is passed inside args.

- Method test_runner() is now calling start_tests() and end_tests(),
  so it is the owner of all methods for test results.
Signed-off-by: NRuda Moura <rmoura@redhat.com>
上级 586cc564
......@@ -44,6 +44,12 @@ class Job(object):
"""
def __init__(self, args=None):
"""
Creates a Job instance.
:param args: an instance of :class:`argparse.Namespace`.
"""
self.args = args
if args is not None:
self.unique_id = args.unique_id or str(uuid.uuid4())
......@@ -114,11 +120,13 @@ class Job(object):
:return: a list of test failures.
"""
failures = []
test_result.start_tests()
for params in params_list:
test_instance = self.run_test(params)
test_result.check_test(test_instance)
if not status.mapping[test_instance.status]:
failures.append(test_instance.name)
test_result.end_tests()
return failures
def _make_test_runner(self):
......@@ -133,11 +141,11 @@ class Job(object):
test_result_class = self.args.test_result
else:
test_result_class = result.HumanTestResult
test_result = test_result_class(self.output_manager,
self.debuglog,
self.loglevel,
len(urls),
self.args)
if self.args is not None:
self.args.test_result_debuglog = self.debuglog
self.args.test_result_loglevel = self.loglevel
self.args.test_result_total = len(urls)
test_result = test_result_class(self.output_manager, self.args)
return test_result
def _run(self, urls=None, multiplex_file=None):
......@@ -189,10 +197,9 @@ class Job(object):
params_list.append(dct)
test_result = self._make_test_result(params_list)
test_runner = self._make_test_runner()
test_result.start_tests()
failures = self.test_runner(params_list, test_result)
test_result.end_tests()
failures = test_runner(params_list, test_result)
# If it's all good so far, set job status to 'PASS'
if self.status == 'RUNNING':
self.status = 'PASS'
......
......@@ -44,9 +44,14 @@ class TestResultJournal(TestResult):
feedback to users from a central place.
"""
def __init__(self, stream=None, debuglog=None, loglevel=None,
tests_total=0, args=None):
TestResult.__init__(self, stream, debuglog, loglevel, tests_total, args)
def __init__(self, stream=None, args=None):
"""
Creates an instance of TestResultJournal.
:param stream: an instance of :class:`avocado.core.output.OutputManager`.
:param args: an instance of :class:`argparse.Namespace`.
"""
TestResult.__init__(self, stream, args)
self.journal_initialized = False
def _init_journal(self, logdir):
......
......@@ -150,20 +150,15 @@ class xUnitTestResult(TestResult):
xUnit Test Result class.
"""
def __init__(self, stream=None, debuglog=None, loglevel=None,
tests_total=0, args=None):
"""
:param stream: Stream where to write output, such as :attr:`sys.stdout`.
:param debuglog: Debug log file path.
:param loglevel: Log level in the :mod:`logging` module.
:param tests_total: Total of tests executed
:param args: :class:`argparse.Namespace` with cmdline arguments.
"""
TestResult.__init__(self, stream, debuglog, loglevel, tests_total, args)
if hasattr(self.args, 'xunit_output'):
self.filename = self.args.xunit_output
else:
self.filename = '-'
def __init__(self, stream=None, args=None):
"""
Creates an instance of xUnitTestResult.
:param stream: an instance of :class:`avocado.core.output.OutputManager`.
:param args: an instance of :class:`argparse.Namespace`.
"""
TestResult.__init__(self, stream, args)
self.filename = getattr(self.args, 'xunit_output', '-')
self.xml = XmlResult()
def start_tests(self):
......
......@@ -22,13 +22,16 @@ class TestResult(object):
Test result class, holder for test result information.
"""
def __init__(self, stream=None, debuglog=None, loglevel=None,
tests_total=0, args=None):
def __init__(self, stream, args):
"""
Creates an instance of TestResult.
:param stream: an instance of :class:`avocado.core.output.OutputManager`.
:param args: an instance of :class:`argparse.Namespace`.
"""
self.stream = stream
self.debuglog = debuglog
self.loglevel = loglevel
self.tests_total = tests_total
self.args = args
self.tests_total = getattr(args, 'test_result_total', 1)
self.tests_run = 0
self.total_time = 0.0
self.passed = []
......@@ -41,6 +44,7 @@ class TestResult(object):
"""
Called once before any tests are executed.
"""
self.tests_run += 1
def end_tests(self):
......@@ -53,7 +57,7 @@ class TestResult(object):
"""
Called when the given test is about to run.
:param test: :class:`avocado.test.Test` instance.
:param test: an instance of :class:`avocado.test.Test`.
"""
pass
......@@ -61,7 +65,7 @@ class TestResult(object):
"""
Called when the given test has been run.
:param test: :class:`avocado.test.Test` instance.
:param test: an instance of :class:`avocado.test.Test`.
"""
self.tests_run += 1
self.total_time += test.time_elapsed
......@@ -70,7 +74,7 @@ class TestResult(object):
"""
Called when a test succeeded.
:param test: :class:`avocado.test.Test` instance.
:param test: an instance of :class:`avocado.test.Test`.
"""
self.passed.append(test)
......@@ -78,7 +82,7 @@ class TestResult(object):
"""
Called when a test had a setup error.
:param test: :class:`avocado.test.Test` instance.
:param test: an instance of :class:`avocado.test.Test`.
"""
self.errors.append(test)
......@@ -86,7 +90,7 @@ class TestResult(object):
"""
Called when a test fails.
:param test: :class:`avocado.test.Test` instance.
:param test: an instance of :class:`avocado.test.Test`.
"""
self.failed.append(test)
......@@ -94,7 +98,7 @@ class TestResult(object):
"""
Called when a test is skipped.
:param test: :class:`avocado.test.Test` instance.
:param test: an instance of :class:`avocado.test.Test`.
"""
self.skipped.append(test)
......@@ -102,7 +106,7 @@ class TestResult(object):
"""
Called when a test had a warning.
:param test: :class:`avocado.test.Test` instance.
:param test: an instance of :class:`avocado.test.Test`.
"""
self.warned.append(test)
......@@ -110,7 +114,7 @@ class TestResult(object):
"""
Called once for a test to check status and report.
:param test: :class:`avocado.test.Test` instance.
:param test: an instance of :class:`avocado.test.Test`.
"""
self.start_test(test)
status_map = {'PASS': self.add_pass,
......@@ -134,8 +138,10 @@ class HumanTestResult(TestResult):
Called once before any tests are executed.
"""
TestResult.start_tests(self)
self.stream.start_file_logging(self.debuglog, self.loglevel)
self.stream.log_header("DEBUG LOG: %s" % self.debuglog)
if hasattr(self.args, 'test_result_debuglog'):
self.stream.start_file_logging(self.args.test_result_debuglog,
self.args.test_result_loglevel)
self.stream.log_header("DEBUG LOG: %s" % self.args.test_result_debuglog)
self.stream.log_header("TOTAL TESTS: %s" % self.tests_total)
def end_tests(self):
......@@ -148,13 +154,14 @@ class HumanTestResult(TestResult):
self.stream.log_header("TOTAL SKIPPED: %d" % len(self.skipped))
self.stream.log_header("TOTAL WARNED: %d" % len(self.warned))
self.stream.log_header("ELAPSED TIME: %.2f s" % self.total_time)
if hasattr(self.args, 'test_result_debuglog'):
self.stream.stop_file_logging()
def start_test(self, test):
"""
Called when the given test is about to run.
:param test: :class:`avocado.test.Test` instance.
:param test: an instance of :class:`avocado.test.Test`.
"""
self.test_label = '(%s/%s) %s: ' % (self.tests_run,
self.tests_total,
......@@ -164,7 +171,7 @@ class HumanTestResult(TestResult):
"""
Called when the given test has been run.
:param test: :class:`avocado.test.Test` instance.
:param test: an instance of :class:`avocado.test.Test`.
"""
TestResult.end_test(self, test)
......@@ -172,7 +179,7 @@ class HumanTestResult(TestResult):
"""
Called when a test succeeded.
:param test: :class:`avocado.test.Test` instance.
:param test: an instance of :class:`avocado.test.Test`.
"""
TestResult.add_pass(self, test)
self.stream.log_pass(self.test_label, test.time_elapsed)
......@@ -181,7 +188,7 @@ class HumanTestResult(TestResult):
"""
Called when a test had a setup error.
:param test: :class:`avocado.test.Test` instance.
:param test: an instance of :class:`avocado.test.Test`.
"""
TestResult.add_error(self, test)
self.stream.log_error(self.test_label, test.time_elapsed)
......@@ -190,7 +197,7 @@ class HumanTestResult(TestResult):
"""
Called when a test fails.
:param test: :class:`avocado.test.Test` instance.
:param test: an instance of :class:`avocado.test.Test`.
"""
TestResult.add_fail(self, test)
self.stream.log_fail(self.test_label, test.time_elapsed)
......@@ -199,7 +206,7 @@ class HumanTestResult(TestResult):
"""
Called when a test is skipped.
:param test: :class:`avocado.test.Test` instance.
:param test: an instance of :class:`avocado.test.Test`.
"""
TestResult.add_skip(self, test)
self.stream.log_skip(self.test_label, test.time_elapsed)
......@@ -208,7 +215,7 @@ class HumanTestResult(TestResult):
"""
Called when a test had a warning.
:param test: :class:`avocado.test.Test` instance.
:param test: an instance of :class:`avocado.test.Test`.
"""
TestResult.add_warn(self, test)
self.stream.log_warn(self.test_label, test.time_elapsed)
......@@ -34,7 +34,7 @@ class xUnitSucceedTest(unittest.TestCase):
def setUp(self):
self.tmpfile = mkstemp()
self.test_result = xunit.xUnitTestResult(tests_total=1)
self.test_result = xunit.xUnitTestResult()
self.test_result.filename = self.tmpfile[1]
self.test_result.start_tests()
self.test1 = test.Test()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册