bpo-45229: Make doctest tests discoverable (GH-28986) (GH-29095)
(cherry picked from commit 8d6740f489fca67a44de165d29d9e0ad86285779)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
diff --git a/Lib/test/test_doctest2.py b/Lib/test/test_doctest2.py
index 347a143..ab8a069 100644
--- a/Lib/test/test_doctest2.py
+++ b/Lib/test/test_doctest2.py
@@ -13,7 +13,6 @@
import sys
import unittest
-from test import support
if sys.flags.optimize >= 2:
raise unittest.SkipTest("Cannot test docstrings with -O2")
@@ -107,17 +106,21 @@ def clsm(cls, val):
"""
return val
-def test_main():
- from test import test_doctest2
- EXPECTED = 19
- f, t = support.run_doctest(test_doctest2)
- if t != EXPECTED:
- raise support.TestFailed("expected %d tests to run, not %d" %
- (EXPECTED, t))
+
+class Test(unittest.TestCase):
+ def test_testmod(self):
+ import doctest, sys
+ EXPECTED = 19
+ f, t = doctest.testmod(sys.modules[__name__])
+ if f:
+ self.fail("%d of %d doctests failed" % (f, t))
+ if t != EXPECTED:
+ self.fail("expected %d tests to run, not %d" % (EXPECTED, t))
+
# Pollute the namespace with a bunch of imported functions and classes,
# to make sure they don't get tested.
from doctest import *
if __name__ == '__main__':
- test_main()
+ unittest.main()