diff --git a/Lib/test/support.py b/Lib/test/support.py --- a/Lib/test/support.py +++ b/Lib/test/support.py @@ -49,7 +49,8 @@ __all__ = [ "threading_cleanup", "reap_children", "cpython_only", "check_impl_detail", "get_attribute", "swap_item", "swap_attr", "requires_IEEE_754", "TestHandler", "Matcher", "can_symlink", "skip_unless_symlink", - "import_fresh_module", "requires_zlib", "PIPE_MAX_SIZE" + "import_fresh_module", "requires_zlib", "PIPE_MAX_SIZE", + "requires_tkinter", ] class Error(Exception): @@ -471,6 +472,16 @@ requires_IEEE_754 = unittest.skipUnless( requires_zlib = unittest.skipUnless(zlib, 'requires zlib') +def requires_tkinter(gui=False): + # Skip test if _tkinter wasn't built. + import_module('_tkinter') + if gui \ + and sys.platform == 'darwin' \ + and 'DISPLAY' not in os.environ: + raise unittest.SkipTest( + "DISPLAY environment variable is required " + "to run Tkinter GUI tests") + is_jython = sys.platform.startswith('java') # Filename used for testing diff --git a/Lib/test/test_tcl.py b/Lib/test/test_tcl.py --- a/Lib/test/test_tcl.py +++ b/Lib/test/test_tcl.py @@ -5,18 +5,17 @@ import sys import os from test import support -# Skip this test if the _tkinter module wasn't built. -_tkinter = support.import_module('_tkinter') +support.requires_tkinter() from tkinter import Tcl -from _tkinter import TclError +from _tkinter import TclError, _flatten class TkinterTest(unittest.TestCase): def testFlattenLen(self): # flatten() - self.assertRaises(TypeError, _tkinter._flatten, True) + self.assertRaises(TypeError, _flatten, True) class TclTest(unittest.TestCase): diff --git a/Lib/test/test_tk.py b/Lib/test/test_tk.py --- a/Lib/test/test_tk.py +++ b/Lib/test/test_tk.py @@ -1,17 +1,10 @@ from test import support -# Skip test if _tkinter wasn't built. -support.import_module('_tkinter') +support.requires_tkinter(gui=True) import tkinter from tkinter.test import runtktests import unittest -try: - tkinter.Button() -except tkinter.TclError as msg: - # assuming tk is not available - raise unittest.SkipTest("tk not available: %s" % msg) - def test_main(enable_gui=False): if enable_gui: if support.use_resources is None: @@ -19,6 +12,14 @@ def test_main(enable_gui=False): elif 'gui' not in support.use_resources: support.use_resources.append('gui') + support.requires('gui') + + try: + tkinter.Button() + except tkinter.TclError as msg: + # assuming tk is not available + raise unittest.SkipTest("tk not available: %s" % msg) + support.run_unittest( *runtktests.get_tests(text=False, packages=['test_tkinter'])) diff --git a/Lib/test/test_ttk_guionly.py b/Lib/test/test_ttk_guionly.py --- a/Lib/test/test_ttk_guionly.py +++ b/Lib/test/test_ttk_guionly.py @@ -2,20 +2,13 @@ import os import unittest from test import support -# Skip this test if _tkinter wasn't built. -support.import_module('_tkinter') +support.requires_tkinter(gui=True) from _tkinter import TclError from tkinter import ttk from tkinter.test import runtktests from tkinter.test.support import get_tk_root -try: - ttk.Button() -except TclError as msg: - # assuming ttk is not available - raise unittest.SkipTest("ttk not available: %s" % msg) - def test_main(enable_gui=False): if enable_gui: if support.use_resources is None: @@ -23,6 +16,14 @@ def test_main(enable_gui=False): elif 'gui' not in support.use_resources: support.use_resources.append('gui') + support.requires('gui') + + try: + ttk.Button() + except TclError as msg: + # assuming ttk is not available + raise unittest.SkipTest("ttk not available: %s" % msg) + try: support.run_unittest( *runtktests.get_tests(text=False, packages=['test_ttk'])) diff --git a/Lib/test/test_ttk_textonly.py b/Lib/test/test_ttk_textonly.py --- a/Lib/test/test_ttk_textonly.py +++ b/Lib/test/test_ttk_textonly.py @@ -1,8 +1,7 @@ import os from test import support -# Skip this test if _tkinter does not exist. -support.import_module('_tkinter') +support.requires_tkinter() from tkinter.test import runtktests