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