blob: b72badff5b7dc9e3ecba6df3a91d05664fbc656e [file] [log] [blame]
Guilherme Polocda93aa2009-01-28 13:09:03 +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.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" % (pkg_name, name[:-len(py_ext)]),
42 fromlist=['']
43 )
44 except test.test_support.ResourceDenied:
45 if gui:
46 raise
47
48def get_tests(text=True, gui=True):
49 """Yield all the tests in the modules found by get_tests_modules.
50
51 If nogui is True, only tests that do not require a GUI will be
52 returned."""
53 attrs = []
54 if text:
55 attrs.append('tests_nogui')
56 if gui:
57 attrs.append('tests_gui')
58 for module in get_tests_modules(gui=gui):
59 for attr in attrs:
60 for test in getattr(module, attr, ()):
61 yield test
62
63if __name__ == "__main__":
64 test.test_support.use_resources = ['gui']
65 test.test_support.run_unittest(*get_tests())