blob: 19769adac72a72c99c9a978c06b5f616721c3099 [file] [log] [blame]
Guilherme Polocda93aa2009-01-28 13:09:03 +00001"""
2Use this module to get and run all tk tests.
3
4Tkinter tests should live in a package inside the directory where this file
5lives, like test_tkinter.
6Extensions also should live in packages following the same rule as above.
7"""
8
9import os
10import sys
11import unittest
Guilherme Polob98cb432009-02-02 20:28:59 +000012import importlib
Ned Deily46268c42011-07-03 21:52:35 -070013import subprocess
Guilherme Polocda93aa2009-01-28 13:09:03 +000014import test.test_support
15
16this_dir_path = os.path.abspath(os.path.dirname(__file__))
17
Ned Deily46268c42011-07-03 21:52:35 -070018_tk_available = None
19
20def 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 Deilyeee10482011-07-03 23:16:49 -070040 import Tkinter
Ned Deily46268c42011-07-03 21:52:35 -070041 try:
42 Tkinter.Button()
Ned Deilyeee10482011-07-03 23:16:49 -070043 except Tkinter.TclError as msg:
Ned Deily46268c42011-07-03 21:52:35 -070044 # assuming tk is not available
45 raise unittest.SkipTest("tk not available: %s" % msg)
46
47 _tk_available = True
48 return
49
Guilherme Polocda93aa2009-01-28 13:09:03 +000050def 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 Polo6785cf02009-01-28 19:23:28 +000056def get_tests_modules(basepath=this_dir_path, gui=True, packages=None):
Guilherme Polocda93aa2009-01-28 13:09:03 +000057 """This will import and yield modules whose names start with test_
Guilherme Polo6785cf02009-01-28 19:23:28 +000058 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 Polocda93aa2009-01-28 13:09:03 +000063 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 Polo6785cf02009-01-28 19:23:28 +000072 if packages and pkg_name not in packages:
73 continue
74
Guilherme Polocda93aa2009-01-28 13:09:03 +000075 filenames = filter(
76 lambda x: x.startswith('test_') and x.endswith(py_ext),
77 filenames)
78
79 for name in filenames:
80 try:
Guilherme Polob98cb432009-02-02 20:28:59 +000081 yield importlib.import_module(
82 ".%s" % name[:-len(py_ext)], pkg_name)
Guilherme Polocda93aa2009-01-28 13:09:03 +000083 except test.test_support.ResourceDenied:
84 if gui:
85 raise
86
Guilherme Polo6785cf02009-01-28 19:23:28 +000087def get_tests(text=True, gui=True, packages=None):
Guilherme Polocda93aa2009-01-28 13:09:03 +000088 """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 Polo6785cf02009-01-28 19:23:28 +000097 for module in get_tests_modules(gui=gui, packages=packages):
Guilherme Polocda93aa2009-01-28 13:09:03 +000098 for attr in attrs:
99 for test in getattr(module, attr, ()):
100 yield test
101
102if __name__ == "__main__":
103 test.test_support.use_resources = ['gui']
104 test.test_support.run_unittest(*get_tests())