|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright (c) 2013-2019 Simon Jagoe |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This software may be modified and distributed under the terms |
| 6 | +# of the 3-clause BSD license. See the LICENSE.txt file for details. |
| 7 | +from __future__ import absolute_import, unicode_literals |
| 8 | + |
| 9 | +from datetime import datetime, timedelta |
| 10 | +import sys |
| 11 | + |
| 12 | +from mock import Mock, patch |
| 13 | +from six.moves import StringIO |
| 14 | + |
| 15 | +from ..plugins.i_result_handler_plugin import IResultHandlerPlugin |
| 16 | +from ..result import ( |
| 17 | + ResultCollector, TestResult, TestCompletionStatus, TestDuration |
| 18 | +) |
| 19 | +from ..testing import unittest |
| 20 | +from . import _test_cases |
| 21 | +from .fixtures import ExcInfoFixture, MockDateTime |
| 22 | + |
| 23 | + |
| 24 | +class TestBuffering(ExcInfoFixture, unittest.TestCase): |
| 25 | + |
| 26 | + @patch('sys.stderr', new_callable=StringIO) |
| 27 | + def test_buffering_stderr(self, stderr): |
| 28 | + # Given |
| 29 | + handler = Mock(spec=IResultHandlerPlugin) |
| 30 | + collector = ResultCollector(buffer=True) |
| 31 | + collector.add_result_handler(handler) |
| 32 | + test_stderr = 'My Test Output' |
| 33 | + start_time = datetime(2015, 12, 23, 8, 14, 12) |
| 34 | + duration = timedelta(seconds=10) |
| 35 | + end_time = start_time + duration |
| 36 | + expected_duration = TestDuration(start_time, end_time) |
| 37 | + case = _test_cases.TestCase('test_method') |
| 38 | + |
| 39 | + # When |
| 40 | + with patch('haas.result.datetime', new=MockDateTime(start_time)): |
| 41 | + collector.startTest(case) |
| 42 | + |
| 43 | + # Then |
| 44 | + self.assertTrue(handler.start_test.called) |
| 45 | + handler.start_test.reset_mock() |
| 46 | + |
| 47 | + # When |
| 48 | + sys.stderr.write(test_stderr) |
| 49 | + |
| 50 | + # Then |
| 51 | + self.assertEqual(stderr.getvalue(), '') |
| 52 | + |
| 53 | + # Given |
| 54 | + with self.exc_info(RuntimeError) as exc_info: |
| 55 | + expected_result = TestResult.from_test_case( |
| 56 | + case, TestCompletionStatus.error, expected_duration, |
| 57 | + exception=exc_info, stderr=test_stderr) |
| 58 | + # When |
| 59 | + with patch('haas.result.datetime', new=MockDateTime(end_time)): |
| 60 | + collector.addError(case, exc_info) |
| 61 | + collector.stopTest(case) |
| 62 | + |
| 63 | + # Then |
| 64 | + self.assertIn(test_stderr, expected_result.exception) |
| 65 | + handler.assert_called_once_with(expected_result) |
| 66 | + |
| 67 | + @patch('sys.stdout', new_callable=StringIO) |
| 68 | + def test_buffering_stdout(self, stdout): |
| 69 | + # Given |
| 70 | + handler = Mock(spec=IResultHandlerPlugin) |
| 71 | + collector = ResultCollector(buffer=True) |
| 72 | + collector.add_result_handler(handler) |
| 73 | + test_stdout = 'My Test Output' |
| 74 | + start_time = datetime(2015, 12, 23, 8, 14, 12) |
| 75 | + duration = timedelta(seconds=10) |
| 76 | + end_time = start_time + duration |
| 77 | + expected_duration = TestDuration(start_time, end_time) |
| 78 | + case = _test_cases.TestCase('test_method') |
| 79 | + |
| 80 | + # When |
| 81 | + with patch('haas.result.datetime', new=MockDateTime(start_time)): |
| 82 | + collector.startTest(case) |
| 83 | + |
| 84 | + # Then |
| 85 | + self.assertTrue(handler.start_test.called) |
| 86 | + handler.start_test.reset_mock() |
| 87 | + |
| 88 | + # When |
| 89 | + sys.stdout.write(test_stdout) |
| 90 | + |
| 91 | + # Then |
| 92 | + self.assertEqual(stdout.getvalue(), '') |
| 93 | + |
| 94 | + # Given |
| 95 | + with self.exc_info(RuntimeError) as exc_info: |
| 96 | + expected_result = TestResult.from_test_case( |
| 97 | + case, TestCompletionStatus.error, expected_duration, |
| 98 | + exception=exc_info, stdout=test_stdout) |
| 99 | + |
| 100 | + # When |
| 101 | + with patch('haas.result.datetime', new=MockDateTime(end_time)): |
| 102 | + collector.addError(case, exc_info) |
| 103 | + collector.stopTest(case) |
| 104 | + |
| 105 | + # Then |
| 106 | + self.assertIn(test_stdout, expected_result.exception) |
| 107 | + handler.assert_called_once_with(expected_result) |
0 commit comments