diff --git a/haas/plugins/parallel_runner.py b/haas/plugins/parallel_runner.py index 803e0cb4..90f40597 100644 --- a/haas/plugins/parallel_runner.py +++ b/haas/plugins/parallel_runner.py @@ -46,9 +46,9 @@ def __call__(self, result): self.results.append(result) -def _run_test_in_process(test_case): +def _run_test_in_process(test_case, buffer): result_handler = ChildResultHandler() - result_collector = ResultCollector(buffer=True) + result_collector = ResultCollector(buffer=buffer) result_collector.add_result_handler(result_handler) runner = BaseTestRunner() runner.run(result_collector, test_case) @@ -131,12 +131,13 @@ def callback(collected_result): error_tests.append(test_case) else: call_result = pool.apply_async( - _run_test_in_process, args=(test_case,), + _run_test_in_process, args=(test_case, result.buffer), callback=callback) call_results.append(call_result) for test_case in error_tests: - collected_result = _run_test_in_process(test_case) + collected_result = _run_test_in_process( + test_case, result.buffer) callback(collected_result) finally: pool.close() diff --git a/haas/tests/test_parallel_runner.py b/haas/tests/test_parallel_runner.py index 55e689ce..a8c72bbc 100644 --- a/haas/tests/test_parallel_runner.py +++ b/haas/tests/test_parallel_runner.py @@ -1,5 +1,8 @@ +from __future__ import print_function + from argparse import ArgumentParser from datetime import datetime, timedelta +import sys import time from mock import Mock, patch @@ -25,6 +28,7 @@ def wait(self, timeout): def apply_async(func, args=None, kwargs=None, callback=None): + """ Run the given function serially (rather than in parallel) """ if args is None: args = () if kwargs is None: @@ -277,6 +281,35 @@ def test_parallel_runner_constructor_initializer(self, pool_class): pool.close.assert_called_once_with() pool.join.assert_called_once_with() + @patch('haas.plugins.parallel_runner.Pool') + def test_parallel_runner_stdout(self, pool_class): + # Given + pool = Mock() + pool_class.return_value = pool + pool.apply_async.side_effect = apply_async + + def test_method(): + print("STDOUT side effect of `test_method()`") + print("STDERR side effect", file=sys.stderr) + test_case = _test_cases.TestCase('test_method') + test_case.test_method = test_method + test_suite = TestSuite([test_case]) + + result_collector = ResultCollector(buffer=False) + runner = ParallelTestRunner() + + patch_stdout = patch('sys.stdout', new=StringIO()) + patch_stderr = patch('sys.stderr', new=StringIO()) + + # When + with patch_stdout as mock_stdout, patch_stderr as mock_stderr: + runner.run(result_collector, test_suite) + + # Then + self.assertEqual( + mock_stdout.getvalue(), "STDOUT side effect of `test_method()`\n") + self.assertEqual(mock_stderr.getvalue(), "STDERR side effect\n") + class TestParallelRunnerImportError(unittest.TestCase):