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.

Revisions

  1. althonos revised this gist Sep 8, 2023. 1 changed file with 64 additions and 39 deletions.
    103 changes: 64 additions & 39 deletions doctest_autoload.py
    Original file line number Diff line number Diff line change
    @@ -1,54 +1,79 @@
    import os
    import sys
    # coding: utf-8
    """Test doctest contained tests in every file of the module.
    """

    import configparser
    import doctest
    import shutil
    import importlib
    import json
    import gzip
    import os
    import pkgutil
    import re
    import warnings
    import shutil
    import sys
    import types
    import warnings
    from unittest import mock

    import __mymodule__


    # Ignore unicode string prefix
    class IgnoreUnicodeChecker(doctest.OutputChecker):
    def check_output(self, want, got, optionflags):
    if sys.version_info[0] > 2:
    want = re.sub("u'(.*?)'", "'\\1'", want)
    want = re.sub('u"(.*?)"', '"\\1"', want)
    return doctest.OutputChecker.check_output(self, want, got, optionflags)


    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('_')):
    def _load_tests_from_module(tests, module, globs, setUp=None, tearDown=None):
    """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, types.ModuleType):
    tests.addTests(doctest.DocTestSuite(attr, globs=globs,
    setUp=setUp, tearDown=tearDown, checker=IgnoreUnicodeChecker()))
    suite = doctest.DocTestSuite(
    attr,
    globs,
    setUp=setUp,
    tearDown=tearDown,
    optionflags=+doctest.ELLIPSIS,
    )
    tests.addTests(suite)
    return tests


    def load_tests(loader, tests, ignore):
    """load_test function used by unittest to find the doctests"""
    """`load_test` function used by unittest to find the doctests."""

    def _setUp(self):
    """setUp method used by the DocTestSuite"""
    pass
    # demonstrate how to load sequences with Biopython without requiring Biopython
    def setUp(self):
    warnings.simplefilter("ignore")
    # ... #

    def _tearDown(self):
    """tearDown method used by the DocTestSuite"""
    pass
    def tearDown(self):
    warnings.simplefilter(warnings.defaultaction)
    # ... #

    # doctests are not compatible with `green`, so we may want to bail out
    # early if `green` is running the tests
    if sys.argv[0].endswith("green"):
    return tests

    globs = {
    # add here the globals that should be available
    # in the tests
    }
    # recursively traverse all library submodules and load tests from them
    packages = [None, __mymodule__]
    for pkg in iter(packages.pop, None):
    for (_, subpkgname, subispkg) in pkgutil.walk_packages(pkg.__path__):
    # do not import __main__ module to avoid side effects!
    if subpkgname == "__main__" or subpkgname.startswith("tests") or subpkgname.startswith("cli"):
    continue
    # import the submodule and add it to the tests
    module = importlib.import_module(".".join([pkg.__name__, subpkgname]))
    globs = {
    # ... #
    }
    tests.addTests(
    doctest.DocTestSuite(
    module,
    globs=globs,
    setUp=setUp,
    tearDown=tearDown,
    optionflags=+doctest.ELLIPSIS,
    )
    )
    # if the submodule is a package, we need to process its submodules
    # as well, so we add it to the package queue
    if subispkg:
    packages.append(module)

    tests = _load_tests_from_module(tests, __mymodule__, globs, _setUp, _tearDown)
    return tests



    def setUpModule():
    warnings.simplefilter('ignore')

    def tearDownModule():
    warnings.simplefilter(warnings.defaultaction)
  2. althonos revised this gist Jul 13, 2017. 1 changed file with 29 additions and 17 deletions.
    46 changes: 29 additions & 17 deletions doctest_autoload.py
    Original file line number Diff line number Diff line change
    @@ -1,42 +1,54 @@
    import os
    import sys
    import doctest
    MODULE_TYPE=type(doctest)
    import shutil
    import re
    import warnings
    import types

    import __mymodule__


    # Ignore unicode string prefix
    class IgnoreUnicodeChecker(doctest.OutputChecker):
    def check_output(self, want, got, optionflags):
    if sys.version_info[0] > 2:
    want = re.sub("u'(.*?)'", "'\\1'", want)
    want = re.sub('u"(.*?)"', '"\\1"', want)
    return doctest.OutputChecker.check_output(self, want, got, optionflags)

    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
    if isinstance(attr, types.ModuleType):
    tests.addTests(doctest.DocTestSuite(attr, globs=globs,
    setUp=setUp, tearDown=tearDown, checker=IgnoreUnicodeChecker()))
    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"""
    """setUp method used by the DocTestSuite"""
    pass

    def _tearDown(self):
    """optional tearDown method used by the DocTestSuite"""
    """tearDown method used by the DocTestSuite"""
    pass

    globs = {
    'foo': 1,
    'bar':'abc',
    # add here the globals that should be available
    # in the tests
    }
    tests = _load_tests_from_module(tests, the_module_im_testing, globs, _setUp, _tearDown)

    tests = _load_tests_from_module(tests, __mymodule__, globs, _setUp, _tearDown)
    return tests



    def setUpModule():
    pass
    warnings.simplefilter('ignore')

    def tearDownModule():
    pass
    warnings.simplefilter(warnings.defaultaction)
  3. althonos created this gist Jan 4, 2017.
    42 changes: 42 additions & 0 deletions doctest_autoload.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    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