blob: 6c974cff5a83f4b0f944b710e5f01bdada83287a [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
Guilherme Polo6785cf02009-01-28 19:23:28 +000022def get_tests_modules(basepath=this_dir_path, gui=True, packages=None):
Guilherme Polocda93aa2009-01-28 13:09:03 +000023 """This will import and yield modules whose names start with test_
Guilherme Polo6785cf02009-01-28 19:23:28 +000024 and are inside packages found in the path starting at basepath.
25
26 If packages is specified it should contain package names that want
27 their tests colleted.
28 """
Guilherme Polocda93aa2009-01-28 13:09:03 +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 Polo6785cf02009-01-28 19:23:28 +000038 if packages and pkg_name not in packages:
39 continue
40
Guilherme Polocda93aa2009-01-28 13:09:03 +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" % (pkg_name, name[:-len(py_ext)]),
49 fromlist=['']
50 )
51 except test.test_support.ResourceDenied:
52 if gui:
53 raise
54
Guilherme Polo6785cf02009-01-28 19:23:28 +000055def get_tests(text=True, gui=True, packages=None):
Guilherme Polocda93aa2009-01-28 13:09:03 +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 Polo6785cf02009-01-28 19:23:28 +000065 for module in get_tests_modules(gui=gui, packages=packages):
Guilherme Polocda93aa2009-01-28 13:09:03 +000066 for attr in attrs:
67 for test in getattr(module, attr, ()):
68 yield test
69
70if __name__ == "__main__":
71 test.test_support.use_resources = ['gui']
72 test.test_support.run_unittest(*get_tests())