blob: 70675940178cbba6138b753224651a6401a978c7 [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
Guilherme Polo9de29af2009-01-28 20:40:48 +000022def get_tests_modules(basepath=this_dir_path, gui=True, packages=None):
Guilherme Polo5f238482009-01-28 14:41:10 +000023 """This will import and yield modules whose names start with test_
Guilherme Polo9de29af2009-01-28 20:40:48 +000024 and are inside packages found in the path starting at basepath.
25
26 If packages is specified it should contain package names that
27 want their tests collected.
28 """
Guilherme Polo5f238482009-01-28 14:41:10 +000029 py_ext = '.py'
30
31 for dirpath, dirnames, filenames in os.walk(basepath):
32 for dirname in list(dirnames):
33 if dirname[0] == '.':
34 dirnames.remove(dirname)
35
36 if is_package(dirpath) and filenames:
37 pkg_name = dirpath[len(basepath) + len(os.sep):].replace('/', '.')
Guilherme Polo9de29af2009-01-28 20:40:48 +000038 if packages and pkg_name not in packages:
39 continue
40
Guilherme Polo5f238482009-01-28 14:41:10 +000041 filenames = filter(
42 lambda x: x.startswith('test_') and x.endswith(py_ext),
43 filenames)
44
45 for name in filenames:
46 try:
47 yield __import__(
48 "%s.%s.%s" % (
49 "tkinter.test",
50 pkg_name,
51 name[:-len(py_ext)]),
52 fromlist=['']
53 )
54 except test.support.ResourceDenied:
55 if gui:
56 raise
57
Guilherme Polo9de29af2009-01-28 20:40:48 +000058def get_tests(text=True, gui=True, packages=None):
Guilherme Polo5f238482009-01-28 14:41:10 +000059 """Yield all the tests in the modules found by get_tests_modules.
60
61 If nogui is True, only tests that do not require a GUI will be
62 returned."""
63 attrs = []
64 if text:
65 attrs.append('tests_nogui')
66 if gui:
67 attrs.append('tests_gui')
Guilherme Polo9de29af2009-01-28 20:40:48 +000068 for module in get_tests_modules(gui=gui, packages=packages):
Guilherme Polo5f238482009-01-28 14:41:10 +000069 for attr in attrs:
70 for test in getattr(module, attr, ()):
71 yield test
72
73if __name__ == "__main__":
74 test.support.use_resources = ['gui']
75 test.support.run_unittest(*get_tests())