blob: 3d3868fee535263d6f53c16e769667ea11140230 [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
12import test.support
13
14this_dir_path = os.path.abspath(os.path.dirname(__file__))
15
16def is_package(path):
17 for name in os.listdir(path):
18 if name in ('__init__.py', '__init__.pyc', '__init.pyo'):
19 return True
20 return False
21
22def get_tests_modules(basepath=this_dir_path, gui=True):
23 """This will import and yield modules whose names start with test_
24 and are inside packages found in the path starting at basepath."""
25 py_ext = '.py'
26
27 for dirpath, dirnames, filenames in os.walk(basepath):
28 for dirname in list(dirnames):
29 if dirname[0] == '.':
30 dirnames.remove(dirname)
31
32 if is_package(dirpath) and filenames:
33 pkg_name = dirpath[len(basepath) + len(os.sep):].replace('/', '.')
34 filenames = filter(
35 lambda x: x.startswith('test_') and x.endswith(py_ext),
36 filenames)
37
38 for name in filenames:
39 try:
40 yield __import__(
41 "%s.%s.%s" % (
42 "tkinter.test",
43 pkg_name,
44 name[:-len(py_ext)]),
45 fromlist=['']
46 )
47 except test.support.ResourceDenied:
48 if gui:
49 raise
50
51def get_tests(text=True, gui=True):
52 """Yield all the tests in the modules found by get_tests_modules.
53
54 If nogui is True, only tests that do not require a GUI will be
55 returned."""
56 attrs = []
57 if text:
58 attrs.append('tests_nogui')
59 if gui:
60 attrs.append('tests_gui')
61 for module in get_tests_modules(gui=gui):
62 for attr in attrs:
63 for test in getattr(module, attr, ()):
64 yield test
65
66if __name__ == "__main__":
67 test.support.use_resources = ['gui']
68 test.support.run_unittest(*get_tests())