Skip to content

Commit 65de808

Browse files
bpo-45229: Make doctest tests discoverable (GH-28986) (GH-29095)
(cherry picked from commit 8d6740f) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent d6afe3b commit 65de808

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

Lib/test/test_doctest.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3110,20 +3110,11 @@ def test_no_trailing_whitespace_stripping():
31103110
patches that contain trailing whitespace. More info on Issue 24746.
31113111
"""
31123112

3113-
######################################################################
3114-
## Main
3115-
######################################################################
3116-
3117-
def test_main():
3118-
# Check the doctest cases in doctest itself:
3119-
ret = support.run_doctest(doctest, verbosity=True)
31203113

3121-
# Check the doctest cases defined here:
3122-
from test import test_doctest
3123-
support.run_doctest(test_doctest, verbosity=True)
3124-
3125-
# Run unittests
3126-
support.run_unittest(__name__)
3114+
def load_tests(loader, tests, pattern):
3115+
tests.addTest(doctest.DocTestSuite(doctest))
3116+
tests.addTest(doctest.DocTestSuite())
3117+
return tests
31273118

31283119

31293120
def test_coverage(coverdir):
@@ -3136,8 +3127,9 @@ def test_coverage(coverdir):
31363127
r.write_results(show_missing=True, summary=True,
31373128
coverdir=coverdir)
31383129

3130+
31393131
if __name__ == '__main__':
31403132
if '-c' in sys.argv:
31413133
test_coverage('/tmp/doctest.cover')
31423134
else:
3143-
test_main()
3135+
unittest.main()

Lib/test/test_doctest2.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import sys
1515
import unittest
16-
from test import support
1716
if sys.flags.optimize >= 2:
1817
raise unittest.SkipTest("Cannot test docstrings with -O2")
1918

@@ -107,17 +106,21 @@ def clsm(cls, val):
107106
"""
108107
return val
109108

110-
def test_main():
111-
from test import test_doctest2
112-
EXPECTED = 19
113-
f, t = support.run_doctest(test_doctest2)
114-
if t != EXPECTED:
115-
raise support.TestFailed("expected %d tests to run, not %d" %
116-
(EXPECTED, t))
109+
110+
class Test(unittest.TestCase):
111+
def test_testmod(self):
112+
import doctest, sys
113+
EXPECTED = 19
114+
f, t = doctest.testmod(sys.modules[__name__])
115+
if f:
116+
self.fail("%d of %d doctests failed" % (f, t))
117+
if t != EXPECTED:
118+
self.fail("expected %d tests to run, not %d" % (EXPECTED, t))
119+
117120

118121
# Pollute the namespace with a bunch of imported functions and classes,
119122
# to make sure they don't get tested.
120123
from doctest import *
121124

122125
if __name__ == '__main__':
123-
test_main()
126+
unittest.main()

0 commit comments

Comments
 (0)