Skip to content

Instantly share code, notes, and snippets.

@althonos
Last active September 8, 2023 09:29
Show Gist options
  • Select an option

  • Save althonos/8298af1c707aa596a53a5431bfbd1a0d to your computer and use it in GitHub Desktop.

Select an option

Save althonos/8298af1c707aa596a53a5431bfbd1a0d to your computer and use it in GitHub Desktop.
Unittest Doctest autoloader: add doctests automatically to the unittest test suites in the tests folder, allowing unit test runners to directly load the doctests.
import doctest
MODULE_TYPE=type(doctest)
import the_module_im_testing
def _load_tests_from_module(tests, module, globs, setUp, tearDown):
"""Load tests from module, iterating through submodules"""
for attr in (getattr(module, x) for x in dir(module) if not x.startswith('_')):
if isinstance(attr, MODULE_TYPE):
try:
tests.addTests(doctest.DocTestSuite(attr, globs=globs,
setUp=setUp, tearDown=tearDown)#, checker=IgnoreUnicodeChecker()))
except ValueError:
pass
return tests
def load_tests(loader, tests, ignore):
"""load_test function used by unittest to find the doctests"""
def _setUp(self):
"""optional setUp method used by the DocTestSuite"""
pass
def _tearDown(self):
"""optional tearDown method used by the DocTestSuite"""
pass
globs = {
'foo': 1,
'bar':'abc',
}
tests = _load_tests_from_module(tests, the_module_im_testing, globs, _setUp, _tearDown)
return tests
def setUpModule():
pass
def tearDownModule():
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment