|
9 | 9 | import types |
10 | 10 | import warnings |
11 | 11 | import tempfile |
| 12 | +import unittest |
12 | 13 |
|
13 | 14 | try: |
14 | 15 | from unittest import mock |
@@ -107,7 +108,7 @@ def _load_tests_from_module(tests, module, globs, setUp=None, tearDown=None): |
107 | 108 | return tests |
108 | 109 |
|
109 | 110 |
|
110 | | -def load_tests(loader, tests, ignore): |
| 111 | +def _load_tests(loader, tests, ignore): |
111 | 112 | """`load_test` function used by unittest to find the doctests.""" |
112 | 113 |
|
113 | 114 | # NB (@althonos): we only test docstrings on Python 3 because it's |
@@ -135,7 +136,6 @@ def tearDown(self): |
135 | 136 |
|
136 | 137 | # recursively traverse all library submodules and load tests from them |
137 | 138 | packages = [None, fs] |
138 | | - |
139 | 139 | for pkg in iter(packages.pop, None): |
140 | 140 | for (_, subpkgname, subispkg) in pkgutil.walk_packages(pkg.__path__): |
141 | 141 | # import the submodule and add it to the tests |
@@ -174,3 +174,30 @@ def tearDown(self): |
174 | 174 | packages.append(module) |
175 | 175 |
|
176 | 176 | 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