|
| 1 | +"""Test hook(s) for Doctests in reStructuredText (.rst) Files.""" |
| 2 | + |
1 | 3 | import doctest |
2 | 4 | import os |
3 | 5 | import unittest |
4 | 6 | import sys |
| 7 | +from typing import Any |
| 8 | + |
| 9 | + |
| 10 | +def load_tests(loader: unittest.TestLoader, tests: unittest.TestSuite, ignorex: Any) -> unittest.TestSuite: |
| 11 | + """ |
| 12 | + Discover and add all reStructuredText (.rst) doctest files within the current directory tree to the test suite. |
| 13 | +
|
| 14 | + Parameters |
| 15 | + ---------- |
| 16 | + loader : unittest.TestLoader |
| 17 | + The test loader instance used to load the tests. |
| 18 | + tests : unittest.TestSuite |
| 19 | + The existing test suite to which doctest suites will be added. |
| 20 | + ignorex : Any |
| 21 | + A placeholder parameter (typically unused) required by the unittest framework. |
5 | 22 |
|
| 23 | + Returns |
| 24 | + ------- |
| 25 | + unittest.TestSuite |
| 26 | + The updated test suite including all discovered .rst doctest files. |
6 | 27 |
|
7 | | -def load_tests(loader, tests, ignorex): |
| 28 | + Notes |
| 29 | + ----- |
| 30 | + This function is used by the unittest framework to discover and include additional doctests |
| 31 | + in the test suite during test discovery. |
| 32 | +
|
| 33 | + Examples |
| 34 | + -------- |
| 35 | + When placed in a test module, unittest will automatically call `load_tests` if it exists. |
| 36 | +
|
| 37 | + >>> import unittest |
| 38 | + >>> suite = unittest.TestSuite() |
| 39 | + >>> loader = unittest.TestLoader() |
| 40 | + >>> load_tests(loader, suite, None) # doctest: +ELLIPSIS |
| 41 | + <unittest.suite.TestSuite tests=[...] |
| 42 | + """ |
8 | 43 | for root, dirs, files in os.walk("."): |
9 | 44 | for f in files: |
10 | 45 | if f.endswith(".rst"): |
11 | 46 | tests.addTests( |
12 | 47 | doctest.DocFileSuite( |
13 | | - os.path.join(root, f), optionflags=doctest.ELLIPSIS |
| 48 | + os.path.join(root, f), |
| 49 | + optionflags=doctest.ELLIPSIS |
14 | 50 | ) |
15 | 51 | ) |
16 | | - |
17 | 52 | return tests |
18 | 53 |
|
19 | 54 |
|
20 | 55 | if __name__ == "__main__": |
21 | 56 | if sys.version_info >= (3, 6): |
22 | 57 | unittest.main() |
| 58 | + |
0 commit comments