Skip to content

Commit 3404061

Browse files
committed
Add a workaround for pytest not supporting the load_tests protocol
1 parent 8388a02 commit 3404061

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

tests/test_doctest.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import types
1010
import warnings
1111
import tempfile
12+
import unittest
1213

1314
try:
1415
from unittest import mock
@@ -107,7 +108,7 @@ def _load_tests_from_module(tests, module, globs, setUp=None, tearDown=None):
107108
return tests
108109

109110

110-
def load_tests(loader, tests, ignore):
111+
def _load_tests(loader, tests, ignore):
111112
"""`load_test` function used by unittest to find the doctests."""
112113

113114
# NB (@althonos): we only test docstrings on Python 3 because it's
@@ -135,7 +136,6 @@ def tearDown(self):
135136

136137
# recursively traverse all library submodules and load tests from them
137138
packages = [None, fs]
138-
139139
for pkg in iter(packages.pop, None):
140140
for (_, subpkgname, subispkg) in pkgutil.walk_packages(pkg.__path__):
141141
# import the submodule and add it to the tests
@@ -174,3 +174,30 @@ def tearDown(self):
174174
packages.append(module)
175175

176176
return tests
177+
178+
179+
# --- Unit test wrapper ------------------------------------------------------
180+
#
181+
# NB (@althonos): Since pytest doesn't support the `load_tests` protocol
182+
# above, we manually build a `unittest.TestCase` using a dedicated test
183+
# method for each doctest. This should be safe to remove when pytest
184+
# supports it, or if we move away from pytest to run tests.
185+
186+
187+
class TestDoctest(unittest.TestCase):
188+
pass
189+
190+
191+
def make_wrapper(x):
192+
def _test_wrapper(self):
193+
x.setUp()
194+
try:
195+
x.runTest()
196+
finally:
197+
x.tearDown()
198+
199+
return _test_wrapper
200+
201+
202+
for x in _load_tests(None, unittest.TestSuite(), False):
203+
setattr(TestDoctest, "test_{}".format(x.id().replace(".", "_")), make_wrapper(x))

0 commit comments

Comments
 (0)