forked from scalative/haas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor-out testcases into separate files
- Loading branch information
Showing
9 changed files
with
1,538 additions
and
1,428 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2013-2019 Simon Jagoe | ||
# All rights reserved. | ||
# | ||
# This software may be modified and distributed under the terms | ||
# of the 3-clause BSD license. See the LICENSE.txt file for details. | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
from datetime import datetime, timedelta | ||
import sys | ||
|
||
from mock import Mock, patch | ||
from six.moves import StringIO | ||
|
||
from ..plugins.i_result_handler_plugin import IResultHandlerPlugin | ||
from ..result import ( | ||
ResultCollector, TestResult, TestCompletionStatus, TestDuration | ||
) | ||
from ..testing import unittest | ||
from . import _test_cases | ||
from .fixtures import ExcInfoFixture, MockDateTime | ||
|
||
|
||
class TestBuffering(ExcInfoFixture, unittest.TestCase): | ||
|
||
@patch('sys.stderr', new_callable=StringIO) | ||
def test_buffering_stderr(self, stderr): | ||
# Given | ||
handler = Mock(spec=IResultHandlerPlugin) | ||
collector = ResultCollector(buffer=True) | ||
collector.add_result_handler(handler) | ||
test_stderr = 'My Test Output' | ||
start_time = datetime(2015, 12, 23, 8, 14, 12) | ||
duration = timedelta(seconds=10) | ||
end_time = start_time + duration | ||
expected_duration = TestDuration(start_time, end_time) | ||
case = _test_cases.TestCase('test_method') | ||
|
||
# When | ||
with patch('haas.result.datetime', new=MockDateTime(start_time)): | ||
collector.startTest(case) | ||
|
||
# Then | ||
self.assertTrue(handler.start_test.called) | ||
handler.start_test.reset_mock() | ||
|
||
# When | ||
sys.stderr.write(test_stderr) | ||
|
||
# Then | ||
self.assertEqual(stderr.getvalue(), '') | ||
|
||
# Given | ||
with self.exc_info(RuntimeError) as exc_info: | ||
expected_result = TestResult.from_test_case( | ||
case, TestCompletionStatus.error, expected_duration, | ||
exception=exc_info, stderr=test_stderr) | ||
# When | ||
with patch('haas.result.datetime', new=MockDateTime(end_time)): | ||
collector.addError(case, exc_info) | ||
collector.stopTest(case) | ||
|
||
# Then | ||
self.assertIn(test_stderr, expected_result.exception) | ||
handler.assert_called_once_with(expected_result) | ||
|
||
@patch('sys.stdout', new_callable=StringIO) | ||
def test_buffering_stdout(self, stdout): | ||
# Given | ||
handler = Mock(spec=IResultHandlerPlugin) | ||
collector = ResultCollector(buffer=True) | ||
collector.add_result_handler(handler) | ||
test_stdout = 'My Test Output' | ||
start_time = datetime(2015, 12, 23, 8, 14, 12) | ||
duration = timedelta(seconds=10) | ||
end_time = start_time + duration | ||
expected_duration = TestDuration(start_time, end_time) | ||
case = _test_cases.TestCase('test_method') | ||
|
||
# When | ||
with patch('haas.result.datetime', new=MockDateTime(start_time)): | ||
collector.startTest(case) | ||
|
||
# Then | ||
self.assertTrue(handler.start_test.called) | ||
handler.start_test.reset_mock() | ||
|
||
# When | ||
sys.stdout.write(test_stdout) | ||
|
||
# Then | ||
self.assertEqual(stdout.getvalue(), '') | ||
|
||
# Given | ||
with self.exc_info(RuntimeError) as exc_info: | ||
expected_result = TestResult.from_test_case( | ||
case, TestCompletionStatus.error, expected_duration, | ||
exception=exc_info, stdout=test_stdout) | ||
|
||
# When | ||
with patch('haas.result.datetime', new=MockDateTime(end_time)): | ||
collector.addError(case, exc_info) | ||
collector.stopTest(case) | ||
|
||
# Then | ||
self.assertIn(test_stdout, expected_result.exception) | ||
handler.assert_called_once_with(expected_result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2013-2019 Simon Jagoe | ||
# All rights reserved. | ||
# | ||
# This software may be modified and distributed under the terms | ||
# of the 3-clause BSD license. See the LICENSE.txt file for details. | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
from testfixtures import ShouldWarn | ||
|
||
from ..result import ResultCollecter | ||
from ..testing import unittest | ||
|
||
|
||
class TestResultCollecterDepricated(unittest.TestCase): | ||
|
||
def test_deprecation_warning(self): | ||
# Given | ||
expected_warning = DeprecationWarning( | ||
'ResultCollecter is deprecated in favour of ResultCollector and ' | ||
'will be removed in the next release.', | ||
) | ||
|
||
# When/Then | ||
with ShouldWarn(expected_warning): | ||
ResultCollecter() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2013-2019 Simon Jagoe | ||
# All rights reserved. | ||
# | ||
# This software may be modified and distributed under the terms | ||
# of the 3-clause BSD license. See the LICENSE.txt file for details. | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
|
||
from ..result import ResultCollector | ||
from ..testing import unittest | ||
from . import _test_cases | ||
from .fixtures import ExcInfoFixture | ||
|
||
|
||
class TestFailfast(ExcInfoFixture, unittest.TestCase): | ||
|
||
def test_failfast_enabled_on_error(self): | ||
# Given | ||
collector = ResultCollector(failfast=True) | ||
self.assertFalse(collector.shouldStop) | ||
case = _test_cases.TestCase('test_method') | ||
|
||
collector.startTest(case) | ||
|
||
# When | ||
with self.exc_info(RuntimeError) as exc_info: | ||
collector.addError(case, exc_info) | ||
|
||
# Then | ||
self.assertTrue(collector.shouldStop) | ||
|
||
def test_failfast_enabled_on_failure(self): | ||
# Given | ||
collector = ResultCollector(failfast=True) | ||
self.assertFalse(collector.shouldStop) | ||
case = _test_cases.TestCase('test_method') | ||
|
||
collector.startTest(case) | ||
|
||
# When | ||
with self.failure_exc_info() as exc_info: | ||
collector.addFailure(case, exc_info) | ||
|
||
# Then | ||
self.assertTrue(collector.shouldStop) | ||
|
||
def test_failfast_enabled_on_unexpected_success(self): | ||
# Given | ||
collector = ResultCollector(failfast=False) | ||
self.assertFalse(collector.shouldStop) | ||
case = _test_cases.TestCase('test_method') | ||
|
||
collector.startTest(case) | ||
|
||
# When | ||
collector.addUnexpectedSuccess(case) | ||
|
||
# Then | ||
self.assertFalse(collector.shouldStop) | ||
|
||
def test_failfast_disabled_on_error(self): | ||
# Given | ||
collector = ResultCollector(failfast=False) | ||
self.assertFalse(collector.shouldStop) | ||
case = _test_cases.TestCase('test_method') | ||
|
||
collector.startTest(case) | ||
|
||
# When | ||
with self.exc_info(RuntimeError) as exc_info: | ||
collector.addError(case, exc_info) | ||
|
||
# Then | ||
self.assertFalse(collector.shouldStop) | ||
|
||
def test_failfast_disabled_on_failure(self): | ||
# Given | ||
collector = ResultCollector(failfast=False) | ||
self.assertFalse(collector.shouldStop) | ||
case = _test_cases.TestCase('test_method') | ||
|
||
collector.startTest(case) | ||
|
||
# When | ||
with self.failure_exc_info() as exc_info: | ||
collector.addFailure(case, exc_info) | ||
|
||
# Then | ||
self.assertFalse(collector.shouldStop) | ||
|
||
def test_failfast_disabled_on_unexpected_success(self): | ||
# Given | ||
collector = ResultCollector(failfast=False) | ||
self.assertFalse(collector.shouldStop) | ||
case = _test_cases.TestCase('test_method') | ||
|
||
collector.startTest(case) | ||
|
||
# When | ||
collector.addUnexpectedSuccess(case) | ||
|
||
# Then | ||
self.assertFalse(collector.shouldStop) |
Oops, something went wrong.