summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/test/__init__.py
blob: bda33e69fea2dc64876ec3d4b36ff33ce73c2d9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os.path
import sys
import unittest


def test_suite(package=__package__, directory=os.path.dirname(__file__)):
    suite = unittest.TestSuite()
    for name in os.listdir(directory):
        if name.startswith('.'):
            continue
        path = os.path.join(directory, name)
        if (os.path.isfile(path) and name.startswith('test_') and
                name.endswith('.py')):
            submodule_name = os.path.splitext(name)[0]
            module_name = "{0}.{1}".format(package, submodule_name)
            __import__(module_name, level=0)
            module_tests = unittest.findTestCases(sys.modules[module_name])
            suite.addTest(module_tests)
        elif os.path.isdir(path):
            package_name = "{0}.{1}".format(package, name)
            __import__(package_name, level=0)
            package_tests = getattr(sys.modules[package_name], 'test_suite')()
            suite.addTest(package_tests)
        else:
            continue
    return suite


if __name__ == '__main__':
    from test.support import run_unittest
    run_unittest(test_suite('importlib.test'))