8000 bpo-2604: Make doctest.DocTestCase reset globs in teardown (GH-31932) · python/cpython@3c60190 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3c60190

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 af341eb commit 3c60190

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
@@ -2159,6 +2159,7 @@ def __init__(self, test, optionflags=0, setUp=None, tearDown=None,
21592159
unittest.TestCase.__init__(self)
21602160
self._dt_optionflags = optionflags
21612161
self._dt_checker = checker
2162+
self._dt_globs = test.globs.copy()
21622163
self._dt_test = test
21632164
self._dt_setUp = setUp
21642165
self._dt_tearDown = tearDown
@@ -2175,7 +2176,9 @@ def tearDown(self):
21752176
if self._dt_tearDown is not None:
21762177
self._dt_tearDown(test)
21772178

2179+
# restore the original globs
21782180
test.globs.clear()
2181+
test.globs.update(self._dt_globs)
21792182

21802183
def runTest(self):
21812184
test = self._dt_test

Lib/test/test_doctest.py

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

31093109

3110+
def test_run_doctestsuite_multiple_times():
3111+
"""
3112+
It was not possible to run the same DocTestSuite multiple times
3113+
http://bugs.python.org/issue2604
3114+
http://bugs.python.org/issue9736
3115+
3116+
>>> import unittest
3117+
>>> import test.sample_doctest
3118+
>>> suite = doctest.DocTestSuite(test.sample_doctest)
3119+
>>> suite.run(unittest.TestResult())
3120+
<unittest.result.TestResult run=9 errors=0 failures=4>
3121+
>>> suite.run(unittest.TestResult())
3122+
<unittest.result.TestResult run=9 errors=0 failures=4>
3123+
"""
3124+
3125+
31103126
def load_tests(loader, tests, pattern):
31113127
tests.addTest(doctest.DocTestSuite(doctest))
31123128
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)
0