From 2ac5beb6c5b79e52795de93ec1e0e055375c156c Mon Sep 17 00:00:00 2001 From: Ioannis Tziakos Date: Thu, 30 Jan 2020 12:13:19 +0000 Subject: [PATCH 1/2] Update TestResult to handle output from tests The TestResult now supports recording the output from stderr and stdout for tests that did not have exception --- haas/result.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/haas/result.py b/haas/result.py index de1a3e10..5de7697f 100644 --- a/haas/result.py +++ b/haas/result.py @@ -101,6 +101,25 @@ def _format_exception(err, is_failure, stdout=None, stderr=None): return ''.join(msgLines) +def _format_output(stdout=None, stderr=None): + """Format stdout and stderr test output into a single string. + + """ + msgLines = [] + if stdout is not None: + if not stdout.endswith('\n'): + stdout += '\n' + msgLines.append(STDOUT_LINE % stdout) + if stderr is not None: + if not stderr.endswith('\n'): + stderr += '\n' + msgLines.append(STDERR_LINE % stderr) + if len(msgLines) > 0: + return ''.join(msgLines) + else: + return None + + class TestDuration(object): """An orderable representation of the duration of an individual test. @@ -214,11 +233,12 @@ class TestResult(object): """ def __init__(self, test_class, test_method_name, status, duration, - exception=None, message=None): + exception=None, message=None, output=None): self.test_class = test_class self.test_method_name = test_method_name self.status = status self.exception = exception + self.output = output self.message = message self.duration = duration @@ -269,13 +289,16 @@ def from_test_case(cls, test_case, status, duration, """ test_class = type(test_case) test_method_name = test_case._testMethodName + output = None if exception is not None: exctype, value, tb = exception is_failure = exctype is test_case.failureException exception = _format_exception( exception, is_failure, stdout, stderr) + elif stdout is not None or stderr is not None: + output = _format_output(stderr, stdout) return cls(test_class, test_method_name, status, duration, - exception, message) + exception, message, output) @property def test(self): From dffb6e3a746fcd637b033261a461feed88cf9507 Mon Sep 17 00:00:00 2001 From: Ioannis Tziakos Date: Thu, 30 Jan 2020 12:15:26 +0000 Subject: [PATCH 2/2] Fix repr and equal for TestResult to consider self.output --- haas/result.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/haas/result.py b/haas/result.py index 5de7697f..713cbd1c 100644 --- a/haas/result.py +++ b/haas/result.py @@ -244,11 +244,11 @@ def __init__(self, test_class, test_method_name, status, duration, def __repr__(self): template = ('<{0} class={1}, method={2}, exc={3!r}, status={4!r}, ' - 'duration={5!r}>') + 'duration={5!r}>, output={6!r}') return template.format( type(self).__name__, self.test_class.__name__, self.test_method_name, self.exception, self.status, - self.duration) + self.duration, self.output) def __eq__(self, other): if not isinstance(other, TestResult): @@ -259,6 +259,7 @@ def __eq__(self, other): self.status == other.status and self.exception == other.exception and self.message == other.message and + self.output == other.output and self.duration == other.duration )