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: |
Ned Deily | eee1048 | 2011-07-03 23:16:49 -0700 | [diff] [blame] | 40 | import Tkinter |
Ned Deily | 46268c4 | 2011-07-03 21:52:35 -0700 | [diff] [blame] | 41 | try: |
| 42 | Tkinter.Button() |
Ned Deily | eee1048 | 2011-07-03 23:16:49 -0700 | [diff] [blame] | 43 | except Tkinter.TclError as msg: |
Ned Deily | 46268c4 | 2011-07-03 21:52:35 -0700 | [diff] [blame] | 44 | # assuming tk is not available |
| 45 | raise unittest.SkipTest("tk not available: %s" % msg) |
| 46 | |
| 47 | _tk_available = True |
| 48 | return |
| 49 | |
Guilherme Polo | cda93aa | 2009-01-28 13:09:03 +0000 | [diff] [blame] | 50 | def is_package(path): |
| 51 | for name in os.listdir(path): |
| 52 | if name in ('__init__.py', '__init__.pyc', '__init.pyo'): |
| 53 | return True |
| 54 | return False |
| 55 | |
Guilherme Polo | 6785cf0 | 2009-01-28 19:23:28 +0000 | [diff] [blame] | 56 | def get_tests_modules(basepath=this_dir_path, gui=True, packages=None): |
Guilherme Polo | cda93aa | 2009-01-28 13:09:03 +0000 | [diff] [blame] | 57 | """This will import and yield modules whose names start with test_ |
Guilherme Polo | 6785cf0 | 2009-01-28 19:23:28 +0000 | [diff] [blame] | 58 | and are inside packages found in the path starting at basepath. |
| 59 | |
| 60 | If packages is specified it should contain package names that want |
| 61 | their tests colleted. |
| 62 | """ |
Guilherme Polo | cda93aa | 2009-01-28 13:09:03 +0000 | [diff] [blame] | 63 | py_ext = '.py' |
| 64 | |
| 65 | for dirpath, dirnames, filenames in os.walk(basepath): |
| 66 | for dirname in list(dirnames): |
| 67 | if dirname[0] == '.': |
| 68 | dirnames.remove(dirname) |
| 69 | |
| 70 | if is_package(dirpath) and filenames: |
| 71 | pkg_name = dirpath[len(basepath) + len(os.sep):].replace('/', '.') |
Guilherme Polo | 6785cf0 | 2009-01-28 19:23:28 +0000 | [diff] [blame] | 72 | if packages and pkg_name not in packages: |
| 73 | continue |
| 74 | |
Guilherme Polo | cda93aa | 2009-01-28 13:09:03 +0000 | [diff] [blame] | 75 | filenames = filter( |
| 76 | lambda x: x.startswith('test_') and x.endswith(py_ext), |
| 77 | filenames) |
| 78 | |
| 79 | for name in filenames: |
| 80 | try: |
Guilherme Polo | b98cb43 | 2009-02-02 20:28:59 +0000 | [diff] [blame] | 81 | yield importlib.import_module( |
| 82 | ".%s" % name[:-len(py_ext)], pkg_name) |
Guilherme Polo | cda93aa | 2009-01-28 13:09:03 +0000 | [diff] [blame] | 83 | except test.test_support.ResourceDenied: |
| 84 | if gui: |
| 85 | raise |
| 86 | |
Guilherme Polo | 6785cf0 | 2009-01-28 19:23:28 +0000 | [diff] [blame] | 87 | def get_tests(text=True, gui=True, packages=None): |
Guilherme Polo | cda93aa | 2009-01-28 13:09:03 +0000 | [diff] [blame] | 88 | """Yield all the tests in the modules found by get_tests_modules. |
| 89 | |
| 90 | If nogui is True, only tests that do not require a GUI will be |
| 91 | returned.""" |
| 92 | attrs = [] |
| 93 | if text: |
| 94 | attrs.append('tests_nogui') |
| 95 | if gui: |
| 96 | attrs.append('tests_gui') |
Guilherme Polo | 6785cf0 | 2009-01-28 19:23:28 +0000 | [diff] [blame] | 97 | for module in get_tests_modules(gui=gui, packages=packages): |
Guilherme Polo | cda93aa | 2009-01-28 13:09:03 +0000 | [diff] [blame] | 98 | for attr in attrs: |
| 99 | for test in getattr(module, attr, ()): |
| 100 | yield test |
| 101 | |
| 102 | if __name__ == "__main__": |
| 103 | test.test_support.use_resources = ['gui'] |
| 104 | test.test_support.run_unittest(*get_tests()) |