blob: e21eca4c45e066ff9078921d436fbb3b5d9cac7f [file] [log] [blame]
Guilherme Polo5f238482009-01-28 14:41:10 +00001"""
2Use this module to get and run all tk tests.
3
4tkinter tests should live in a package inside the directory where this file
5lives, like test_tkinter.
6Extensions also should live in packages following the same rule as above.
7"""
8
9import os
10import sys
11import unittest
Benjamin Peterson063ff652009-02-06 03:01:24 +000012import importlib
Guilherme Polo5f238482009-01-28 14:41:10 +000013import test.support
14
15this_dir_path = os.path.abspath(os.path.dirname(__file__))
16
17def is_package(path):
18 for name in os.listdir(path):
19 if name in ('__init__.py', '__init__.pyc', '__init.pyo'):
20 return True
21 return False
22
Guilherme Polo9de29af2009-01-28 20:40:48 +000023def get_tests_modules(basepath=this_dir_path, gui=True, packages=None):
Guilherme Polo5f238482009-01-28 14:41:10 +000024 """This will import and yield modules whose names start with test_
Guilherme Polo9de29af2009-01-28 20:40:48 +000025 and are inside packages found in the path starting at basepath.
26
27 If packages is specified it should contain package names that
28 want their tests collected.
29 """
Guilherme Polo5f238482009-01-28 14:41:10 +000030 py_ext = '.py'
31
32 for dirpath, dirnames, filenames in os.walk(basepath):
33 for dirname in list(dirnames):
34 if dirname[0] == '.':
35 dirnames.remove(dirname)
36
37 if is_package(dirpath) and filenames:
38 pkg_name = dirpath[len(basepath) + len(os.sep):].replace('/', '.')
Guilherme Polo9de29af2009-01-28 20:40:48 +000039 if packages and pkg_name not in packages:
40 continue
41
Guilherme Polo5f238482009-01-28 14:41:10 +000042 filenames = filter(
43 lambda x: x.startswith('test_') and x.endswith(py_ext),
44 filenames)
45
46 for name in filenames:
47 try:
Benjamin Peterson063ff652009-02-06 03:01:24 +000048 yield importlib.import_module(
Guilherme Polob41bc912009-02-06 23:04:44 +000049 ".%s.%s" % (pkg_name, name[:-len(py_ext)]),
50 "tkinter.test")
Guilherme Polo5f238482009-01-28 14:41:10 +000051 except test.support.ResourceDenied:
52 if gui:
53 raise
54
Guilherme Polo9de29af2009-01-28 20:40:48 +000055def get_tests(text=True, gui=True, packages=None):
Guilherme Polo5f238482009-01-28 14:41:10 +000056 """Yield all the tests in the modules found by get_tests_modules.
57
58 If nogui is True, only tests that do not require a GUI will be
59 returned."""
60 attrs = []
61 if text:
62 attrs.append('tests_nogui')
63 if gui:
64 attrs.append('tests_gui')
Guilherme Polo9de29af2009-01-28 20:40:48 +000065 for module in get_tests_modules(gui=gui, packages=packages):
Guilherme Polo5f238482009-01-28 14:41:10 +000066 for attr in attrs:
67 for test in getattr(module, attr, ()):
68 yield test
69
70if __name__ == "__main__":
71 test.support.use_resources = ['gui']
72 test.support.run_unittest(*get_tests())