Skip to content

Commit f163ad2

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 <jelle.zijlstra@gmail.com> (cherry picked from commit 7ba7eae) Co-authored-by: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
1 parent 8146e6b commit f163ad2

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
@@ -3126,6 +3126,22 @@ def test_no_trailing_whitespace_stripping():
31263126
"""
31273127

31283128

3129+
def test_run_doctestsuite_multiple_times():
3130+
"""
3131+
It was not possible to run the same DocTestSuite multiple times
3132+
http://bugs.python.org/issue2604
3133+
http://bugs.python.org/issue9736
3134+
3135+
>>> import unittest
3136+
>>> import test.sample_doctest
3137+
>>> suite = doctest.DocTestSuite(test.sample_doctest)
3138+
>>> suite.run(unittest.TestResult())
3139+
<unittest.result.TestResult run=9 errors=0 failures=4>
3140+
>>> suite.run(unittest.TestResult())
3141+
<unittest.result.TestResult run=9 errors=0 failures=4>
3142+
"""
3143+
3144+
31293145
def load_tests(loader, tests, pattern):
31303146
tests.addTest(doctest.DocTestSuite(doctest))
31313147
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)