1 | n/a | """ |
---|
2 | n/a | Use this module to get and run all tk tests. |
---|
3 | n/a | |
---|
4 | n/a | tkinter tests should live in a package inside the directory where this file |
---|
5 | n/a | lives, like test_tkinter. |
---|
6 | n/a | Extensions also should live in packages following the same rule as above. |
---|
7 | n/a | """ |
---|
8 | n/a | |
---|
9 | n/a | import os |
---|
10 | n/a | import importlib |
---|
11 | n/a | import test.support |
---|
12 | n/a | |
---|
13 | n/a | this_dir_path = os.path.abspath(os.path.dirname(__file__)) |
---|
14 | n/a | |
---|
15 | n/a | def is_package(path): |
---|
16 | n/a | for name in os.listdir(path): |
---|
17 | n/a | if name in ('__init__.py', '__init__.pyc'): |
---|
18 | n/a | return True |
---|
19 | n/a | return False |
---|
20 | n/a | |
---|
21 | n/a | def get_tests_modules(basepath=this_dir_path, gui=True, packages=None): |
---|
22 | n/a | """This will import and yield modules whose names start with test_ |
---|
23 | n/a | and are inside packages found in the path starting at basepath. |
---|
24 | n/a | |
---|
25 | n/a | If packages is specified it should contain package names that |
---|
26 | n/a | want their tests collected. |
---|
27 | n/a | """ |
---|
28 | n/a | py_ext = '.py' |
---|
29 | n/a | |
---|
30 | n/a | for dirpath, dirnames, filenames in os.walk(basepath): |
---|
31 | n/a | for dirname in list(dirnames): |
---|
32 | n/a | if dirname[0] == '.': |
---|
33 | n/a | dirnames.remove(dirname) |
---|
34 | n/a | |
---|
35 | n/a | if is_package(dirpath) and filenames: |
---|
36 | n/a | pkg_name = dirpath[len(basepath) + len(os.sep):].replace('/', '.') |
---|
37 | n/a | if packages and pkg_name not in packages: |
---|
38 | n/a | continue |
---|
39 | n/a | |
---|
40 | n/a | filenames = filter( |
---|
41 | n/a | lambda x: x.startswith('test_') and x.endswith(py_ext), |
---|
42 | n/a | filenames) |
---|
43 | n/a | |
---|
44 | n/a | for name in filenames: |
---|
45 | n/a | try: |
---|
46 | n/a | yield importlib.import_module( |
---|
47 | n/a | ".%s.%s" % (pkg_name, name[:-len(py_ext)]), |
---|
48 | n/a | "tkinter.test") |
---|
49 | n/a | except test.support.ResourceDenied: |
---|
50 | n/a | if gui: |
---|
51 | n/a | raise |
---|
52 | n/a | |
---|
53 | n/a | def get_tests(text=True, gui=True, packages=None): |
---|
54 | n/a | """Yield all the tests in the modules found by get_tests_modules. |
---|
55 | n/a | |
---|
56 | n/a | If nogui is True, only tests that do not require a GUI will be |
---|
57 | n/a | returned.""" |
---|
58 | n/a | attrs = [] |
---|
59 | n/a | if text: |
---|
60 | n/a | attrs.append('tests_nogui') |
---|
61 | n/a | if gui: |
---|
62 | n/a | attrs.append('tests_gui') |
---|
63 | n/a | for module in get_tests_modules(gui=gui, packages=packages): |
---|
64 | n/a | for attr in attrs: |
---|
65 | n/a | for test in getattr(module, attr, ()): |
---|
66 | n/a | yield test |
---|
67 | n/a | |
---|
68 | n/a | if __name__ == "__main__": |
---|
69 | n/a | test.support.run_unittest(*get_tests()) |
---|