Guilherme Polo | cda93aa | 2009-01-28 13:09:03 +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 |
| 10 | import sys |
| 11 | import unittest |
| 12 | import test.test_support |
| 13 | |
| 14 | this_dir_path = os.path.abspath(os.path.dirname(__file__)) |
| 15 | |
| 16 | def 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 | |
| 22 | def 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 | |
| 48 | def 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 | |
| 63 | if __name__ == "__main__": |
| 64 | test.test_support.use_resources = ['gui'] |
| 65 | test.test_support.run_unittest(*get_tests()) |