diff options
author | Michael Foord <michael@python.org> | 2011-03-17 17:44:18 (GMT) |
---|---|---|
committer | Michael Foord <michael@python.org> | 2011-03-17 17:44:18 (GMT) |
commit | 42ec7cb1050c5d1bdf5d504903fabe8bb372b5da (patch) | |
tree | 6cb48e730f65fb53688baef5dac66a8bed95538f /Lib/unittest/suite.py | |
parent | b2760a0bc5166f61180e5ed7e25bcf4132c68e62 (diff) | |
download | cpython-42ec7cb1050c5d1bdf5d504903fabe8bb372b5da.zip cpython-42ec7cb1050c5d1bdf5d504903fabe8bb372b5da.tar.gz cpython-42ec7cb1050c5d1bdf5d504903fabe8bb372b5da.tar.bz2 |
Issue #10979. unittest stdout buffering now works with class and module setup and teardown.
Diffstat (limited to 'Lib/unittest/suite.py')
-rw-r--r-- | Lib/unittest/suite.py | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/Lib/unittest/suite.py b/Lib/unittest/suite.py index 77ce089..38bd6b8 100644 --- a/Lib/unittest/suite.py +++ b/Lib/unittest/suite.py @@ -8,6 +8,11 @@ from . import util __unittest = True +def _call_if_exists(parent, attr): + func = getattr(parent, attr, lambda: None) + func() + + class BaseTestSuite(object): """A simple test suite that doesn't provide class or module shared fixtures. """ @@ -133,6 +138,7 @@ class TestSuite(BaseTestSuite): setUpClass = getattr(currentClass, 'setUpClass', None) if setUpClass is not None: + _call_if_exists(result, '_setupStdout') try: setUpClass() except Exception as e: @@ -142,6 +148,8 @@ class TestSuite(BaseTestSuite): className = util.strclass(currentClass) errorName = 'setUpClass (%s)' % className self._addClassOrModuleLevelException(result, e, errorName) + finally: + _call_if_exists(result, '_restoreStdout') def _get_previous_module(self, result): previousModule = None @@ -167,6 +175,7 @@ class TestSuite(BaseTestSuite): return setUpModule = getattr(module, 'setUpModule', None) if setUpModule is not None: + _call_if_exists(result, '_setupStdout') try: setUpModule() except Exception as e: @@ -175,6 +184,8 @@ class TestSuite(BaseTestSuite): result._moduleSetUpFailed = True errorName = 'setUpModule (%s)' % currentModule self._addClassOrModuleLevelException(result, e, errorName) + finally: + _call_if_exists(result, '_restoreStdout') def _addClassOrModuleLevelException(self, result, exception, errorName): error = _ErrorHolder(errorName) @@ -198,6 +209,7 @@ class TestSuite(BaseTestSuite): tearDownModule = getattr(module, 'tearDownModule', None) if tearDownModule is not None: + _call_if_exists(result, '_setupStdout') try: tearDownModule() except Exception as e: @@ -205,6 +217,8 @@ class TestSuite(BaseTestSuite): raise errorName = 'tearDownModule (%s)' % previousModule self._addClassOrModuleLevelException(result, e, errorName) + finally: + _call_if_exists(result, '_restoreStdout') def _tearDownPreviousClass(self, test, result): previousClass = getattr(result, '_previousTestClass', None) @@ -220,6 +234,7 @@ class TestSuite(BaseTestSuite): tearDownClass = getattr(previousClass, 'tearDownClass', None) if tearDownClass is not None: + _call_if_exists(result, '_setupStdout') try: tearDownClass() except Exception as e: @@ -228,7 +243,8 @@ class TestSuite(BaseTestSuite): className = util.strclass(previousClass) errorName = 'tearDownClass (%s)' % className self._addClassOrModuleLevelException(result, e, errorName) - + finally: + _call_if_exists(result, '_restoreStdout') class _ErrorHolder(object): |