Skip to content

Commit 7ba7eae

Browse files
bpo-2604: Make doctest.DocTestCase reset globs in teardown (GH-31932)
Co-authored-by: Piet Delport Co-authored-by: Hugo Lopes Tavares Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 4aea656 commit 7ba7eae

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

Lib/doctest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,6 +2171,7 @@ def __init__(self, test, optionflags=0, setUp=None, tearDown=None,
21712171
unittest.TestCase.__init__(self)
21722172
self._dt_optionflags = optionflags
21732173
self._dt_checker = checker
2174+
self._dt_globs = test.globs.copy()
21742175
self._dt_test = test
21752176
self._dt_setUp = setUp
21762177
self._dt_tearDown = tearDown
@@ -2187,7 +2188,9 @@ def tearDown(self):
21872188
if self._dt_tearDown is not None:
21882189
self._dt_tearDown(test)
21892190

2191+
# restore the original globs
21902192
test.globs.clear()
2193+
test.globs.update(self._dt_globs)
21912194

21922195
def runTest(self):
21932196
test = self._dt_test

Lib/test/test_doctest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3135,6 +3135,22 @@ def test_no_trailing_whitespace_stripping():
31353135
"""
31363136

31373137

3138+
def test_run_doctestsuite_multiple_times():
3139+
"""
3140+
It was not possible to run the same DocTestSuite multiple times
3141+
http://bugs.python.org/issue2604
3142+
http://bugs.python.org/issue9736
3143+
3144+
>>> import unittest
3145+
>>> import test.sample_doctest
3146+
>>> suite = doctest.DocTestSuite(test.sample_doctest)
3147+
>>> suite.run(unittest.TestResult())
3148+
<unittest.result.TestResult run=9 errors=0 failures=4>
3149+
>>> suite.run(unittest.TestResult())
3150+
<unittest.result.TestResult run=9 errors=0 failures=4>
3151+
"""
3152+
3153+
31383154
def load_tests(loader, tests, pattern):
31393155
tests.addTest(doctest.DocTestSuite(doctest))
31403156
tests.addTest(doctest.DocTestSuite())
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug where doctests using globals would fail when run multiple times.

0 commit comments

Comments
 (0)