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 |
Guilherme Polo | b98cb43 | 2009-02-02 20:28:59 +0000 | [diff] [blame] | 12 | import importlib |
Ned Deily | 46268c4 | 2011-07-03 21:52:35 -0700 | [diff] [blame^] | 13 | import subprocess |
Guilherme Polo | cda93aa | 2009-01-28 13:09:03 +0000 | [diff] [blame] | 14 | import test.test_support |
| 15 | |
| 16 | this_dir_path = os.path.abspath(os.path.dirname(__file__)) |
| 17 | |
Ned Deily | 46268c4 | 2011-07-03 21:52:35 -0700 | [diff] [blame^] | 18 | _tk_available = None |
| 19 | |
| 20 | def check_tk_availability(): |
| 21 | """Check that Tk is installed and available.""" |
| 22 | global _tk_available |
| 23 | |
| 24 | if _tk_available is not None: |
| 25 | return |
| 26 | |
| 27 | if sys.platform == 'darwin': |
| 28 | # The Aqua Tk implementations on OS X can abort the process if |
| 29 | # being called in an environment where a window server connection |
| 30 | # cannot be made, for instance when invoked by a buildbot or ssh |
| 31 | # process not running under the same user id as the current console |
| 32 | # user. Instead, try to initialize Tk under a subprocess. |
| 33 | p = subprocess.Popen( |
| 34 | [sys.executable, '-c', 'import Tkinter; Tkinter.Button()'], |
| 35 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 36 | stderr = test.test_support.strip_python_stderr(p.communicate()[1]) |
| 37 | if stderr or p.returncode: |
| 38 | raise unittest.SkipTest("tk cannot be initialized: %s" % stderr) |
| 39 | else: |
| 40 | try: |
| 41 | Tkinter.Button() |
| 42 | except tkinter.TclError as msg: |
| 43 | # assuming tk is not available |
| 44 | raise unittest.SkipTest("tk not available: %s" % msg) |
| 45 | |
| 46 | _tk_available = True |
| 47 | return |
| 48 | |
Guilherme Polo | cda93aa | 2009-01-28 13:09:03 +0000 | [diff] [blame] | 49 | def is_package(path): |
| 50 | for name in os.listdir(path): |
| 51 | if name in ('__init__.py', '__init__.pyc', '__init.pyo'): |
| 52 | return True |
| 53 | return False |
| 54 | |
Guilherme Polo | 6785cf0 | 2009-01-28 19:23:28 +0000 | [diff] [blame] | 55 | def get_tests_modules(basepath=this_dir_path, gui=True, packages=None): |
Guilherme Polo | cda93aa | 2009-01-28 13:09:03 +0000 | [diff] [blame] | 56 | """This will import and yield modules whose names start with test_ |
Guilherme Polo | 6785cf0 | 2009-01-28 19:23:28 +0000 | [diff] [blame] | 57 | and are inside packages found in the path starting at basepath. |
| 58 | |
| 59 | If packages is specified it should contain package names that want |
| 60 | their tests colleted. |
| 61 | """ |
Guilherme Polo | cda93aa | 2009-01-28 13:09:03 +0000 | [diff] [blame] | 62 | py_ext = '.py' |
| 63 | |
| 64 | for dirpath, dirnames, filenames in os.walk(basepath): |
| 65 | for dirname in list(dirnames): |
| 66 | if dirname[0] == '.': |
| 67 | dirnames.remove(dirname) |
| 68 | |
| 69 | if is_package(dirpath) and filenames: |
| 70 | pkg_name = dirpath[len(basepath) + len(os.sep):].replace('/', '.') |
Guilherme Polo | 6785cf0 | 2009-01-28 19:23:28 +0000 | [diff] [blame] | 71 | if packages and pkg_name not in packages: |
| 72 | continue |
| 73 | |
Guilherme Polo | cda93aa | 2009-01-28 13:09:03 +0000 | [diff] [blame] | 74 | filenames = filter( |
| 75 | lambda x: x.startswith('test_') and x.endswith(py_ext), |
| 76 | filenames) |
| 77 | |
| 78 | for name in filenames: |
| 79 | try: |
Guilherme Polo | b98cb43 | 2009-02-02 20:28:59 +0000 | [diff] [blame] | 80 | yield importlib.import_module( |
| 81 | ".%s" % name[:-len(py_ext)], pkg_name) |
Guilherme Polo | cda93aa | 2009-01-28 13:09:03 +0000 | [diff] [blame] | 82 | except test.test_support.ResourceDenied: |
| 83 | if gui: |
| 84 | raise |
| 85 | |
Guilherme Polo | 6785cf0 | 2009-01-28 19:23:28 +0000 | [diff] [blame] | 86 | def get_tests(text=True, gui=True, packages=None): |
Guilherme Polo | cda93aa | 2009-01-28 13:09:03 +0000 | [diff] [blame] | 87 | """Yield all the tests in the modules found by get_tests_modules. |
| 88 | |
| 89 | If nogui is True, only tests that do not require a GUI will be |
| 90 | returned.""" |
| 91 | attrs = [] |
| 92 | if text: |
| 93 | attrs.append('tests_nogui') |
| 94 | if gui: |
| 95 | attrs.append('tests_gui') |
Guilherme Polo | 6785cf0 | 2009-01-28 19:23:28 +0000 | [diff] [blame] | 96 | for module in get_tests_modules(gui=gui, packages=packages): |
Guilherme Polo | cda93aa | 2009-01-28 13:09:03 +0000 | [diff] [blame] | 97 | for attr in attrs: |
| 98 | for test in getattr(module, attr, ()): |
| 99 | yield test |
| 100 | |
| 101 | if __name__ == "__main__": |
| 102 | test.test_support.use_resources = ['gui'] |
| 103 | test.test_support.run_unittest(*get_tests()) |