diff --git a/Lib/test/test_tk.py b/Lib/test/test_tk.py deleted file mode 100644 --- a/Lib/test/test_tk.py +++ /dev/null @@ -1,18 +0,0 @@ -from test import support -# Skip test if _tkinter wasn't built. -support.import_module('_tkinter') - -# Make sure tkinter._fix runs to set up the environment -support.import_fresh_module('tkinter') - -# Skip test if tk cannot be initialized. -support.requires('gui') - -from tkinter.test import runtktests - -def test_main(): - support.run_unittest( - *runtktests.get_tests(text=False, packages=['test_tkinter'])) - -if __name__ == '__main__': - test_main() diff --git a/Lib/test/test_tkinter.py b/Lib/test/test_tkinter.py new file mode 100644 --- /dev/null +++ b/Lib/test/test_tkinter.py @@ -0,0 +1,5 @@ +import unittest +from tkinter.test import load_tests + +if __name__ == '__main__': + unittest.main() diff --git a/Lib/test/test_ttk_guionly.py b/Lib/test/test_ttk_guionly.py deleted file mode 100644 --- a/Lib/test/test_ttk_guionly.py +++ /dev/null @@ -1,37 +0,0 @@ -import os -import unittest -from test import support - -# Skip this test if _tkinter wasn't built. -support.import_module('_tkinter') - -# Make sure tkinter._fix runs to set up the environment -tkinter = support.import_fresh_module('tkinter') - -# Skip test if tk cannot be initialized. -support.requires('gui') - -from _tkinter import TclError -from tkinter import ttk -from tkinter.test import runtktests - -root = None -try: - root = tkinter.Tk() - button = ttk.Button(root) - button.destroy() - del button -except TclError as msg: - # assuming ttk is not available - raise unittest.SkipTest("ttk not available: %s" % msg) -finally: - if root is not None: - root.destroy() - del root - -def test_main(): - support.run_unittest( - *runtktests.get_tests(text=False, packages=['test_ttk'])) - -if __name__ == '__main__': - test_main() diff --git a/Lib/test/test_ttk_textonly.py b/Lib/test/test_ttk_textonly.py deleted file mode 100644 --- a/Lib/test/test_ttk_textonly.py +++ /dev/null @@ -1,17 +0,0 @@ -import os -from test import support - -# Skip this test if _tkinter does not exist. -support.import_module('_tkinter') - -# Make sure tkinter._fix runs to set up the environment -support.import_fresh_module('tkinter') - -from tkinter.test import runtktests - -def test_main(): - support.run_unittest( - *runtktests.get_tests(gui=False, packages=['test_ttk'])) - -if __name__ == '__main__': - test_main() diff --git a/Lib/tkinter/test/__init__.py b/Lib/tkinter/test/__init__.py --- a/Lib/tkinter/test/__init__.py +++ b/Lib/tkinter/test/__init__.py @@ -0,0 +1,8 @@ +import os +from test.support import load_package_tests, import_module + +# Skip if _tkinter is not available +import_module('tkinter') + +def load_tests(*args): + return load_package_tests(os.path.dirname(__file__), *args) diff --git a/Lib/tkinter/test/__main__.py b/Lib/tkinter/test/__main__.py new file mode 100644 --- /dev/null +++ b/Lib/tkinter/test/__main__.py @@ -0,0 +1,4 @@ +from . import load_tests +import unittest + +unittest.main() diff --git a/Lib/tkinter/test/runtktests.py b/Lib/tkinter/test/runtktests.py deleted file mode 100644 --- a/Lib/tkinter/test/runtktests.py +++ /dev/null @@ -1,71 +0,0 @@ -""" -Use this module to get and run all tk tests. - -tkinter tests should live in a package inside the directory where this file -lives, like test_tkinter. -Extensions also should live in packages following the same rule as above. -""" - -import os -import sys -import unittest -import importlib -import test.support - -this_dir_path = os.path.abspath(os.path.dirname(__file__)) - -def is_package(path): - for name in os.listdir(path): - if name in ('__init__.py', '__init__.pyc', '__init.pyo'): - return True - return False - -def get_tests_modules(basepath=this_dir_path, gui=True, packages=None): - """This will import and yield modules whose names start with test_ - and are inside packages found in the path starting at basepath. - - If packages is specified it should contain package names that - want their tests collected. - """ - py_ext = '.py' - - for dirpath, dirnames, filenames in os.walk(basepath): - for dirname in list(dirnames): - if dirname[0] == '.': - dirnames.remove(dirname) - - if is_package(dirpath) and filenames: - pkg_name = dirpath[len(basepath) + len(os.sep):].replace('/', '.') - if packages and pkg_name not in packages: - continue - - filenames = filter( - lambda x: x.startswith('test_') and x.endswith(py_ext), - filenames) - - for name in filenames: - try: - yield importlib.import_module( - ".%s.%s" % (pkg_name, name[:-len(py_ext)]), - "tkinter.test") - except test.support.ResourceDenied: - if gui: - raise - -def get_tests(text=True, gui=True, packages=None): - """Yield all the tests in the modules found by get_tests_modules. - - If nogui is True, only tests that do not require a GUI will be - returned.""" - attrs = [] - if text: - attrs.append('tests_nogui') - if gui: - attrs.append('tests_gui') - for module in get_tests_modules(gui=gui, packages=packages): - for attr in attrs: - for test in getattr(module, attr, ()): - yield test - -if __name__ == "__main__": - test.support.run_unittest(*get_tests()) diff --git a/Lib/tkinter/test/support.py b/Lib/tkinter/test/support.py --- a/Lib/tkinter/test/support.py +++ b/Lib/tkinter/test/support.py @@ -7,6 +7,8 @@ @classmethod def setUpClass(cls): + requires('gui', + "{} requires the 'gui' resource".format(cls.__qualname__)) cls._old_support_default_root = tkinter._support_default_root destroy_default_root() tkinter.NoDefaultRoot() @@ -102,3 +104,24 @@ if isinstance(expected, (str, tkinter.Widget)): return str(actual) == str(expected) return False + +def check_ttk_availability(name='test'): + requires('gui', "{} requires the 'gui' resource".format(name)) + # Skip if we can't create a ttk Button + root = None + try: + old_support_default = tkinter._support_default_root + tkinter._support_default_root = 0 + root = tkinter.Tk() + btn = tkinter.ttk.Button(root) + except Exception as e: + raise unittest.SkipTest( + 'ttk not available: {}: {}'.format(type(e).__name__, str(e)) + ) + else: + # clean up + btn.destroy() + finally: + if root is not None: + root.destroy() + tkinter._support_default_root = old_support_default diff --git a/Lib/test/test_tcl.py b/Lib/tkinter/test/test_tcl.py rename from Lib/test/test_tcl.py rename to Lib/tkinter/test/test_tcl.py --- a/Lib/test/test_tcl.py +++ b/Lib/tkinter/test/test_tcl.py @@ -2,15 +2,12 @@ import sys import os from test import support +from tkinter.test.support import get_tk_patchlevel -# Skip this test if the _tkinter module wasn't built. -_tkinter = support.import_module('_tkinter') +import tkinter +import _tkinter -# Make sure tkinter._fix runs to set up the environment -support.import_fresh_module('tkinter') - -from tkinter import Tcl -from _tkinter import TclError +from tkinter import Tcl, TclError try: from _testcapi import INT_MAX, PY_SSIZE_T_MAX @@ -25,21 +22,6 @@ pass tcl_version = tuple(tcl_version) -_tk_patchlevel = None -def get_tk_patchlevel(): - global _tk_patchlevel - if _tk_patchlevel is None: - tcl = Tcl() - patchlevel = [] - for x in tcl.call('info', 'patchlevel').split('.'): - try: - x = int(x, 10) - except ValueError: - x = -1 - patchlevel.append(x) - _tk_patchlevel = tuple(patchlevel) - return _tk_patchlevel - class TkinterTest(unittest.TestCase): @@ -607,8 +589,5 @@ print('patchlevel =', tcl.call('info', 'patchlevel')) -def test_main(): - support.run_unittest(TclTest, TkinterTest, BigmemTclTest) - if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/tkinter/test/test_tkinter/__init__.py b/Lib/tkinter/test/test_tkinter/__init__.py --- a/Lib/tkinter/test/test_tkinter/__init__.py +++ b/Lib/tkinter/test/test_tkinter/__init__.py @@ -0,0 +1,5 @@ +import os +from test.support import load_package_tests + +def load_tests(*args): + return load_package_tests(os.path.dirname(__file__), *args) diff --git a/Lib/tkinter/test/test_tkinter/__main__.py b/Lib/tkinter/test/test_tkinter/__main__.py new file mode 100644 --- /dev/null +++ b/Lib/tkinter/test/test_tkinter/__main__.py @@ -0,0 +1,4 @@ +from . import load_tests +import unittest + +unittest.main() diff --git a/Lib/tkinter/test/test_tkinter/test_font.py b/Lib/tkinter/test/test_tkinter/test_font.py --- a/Lib/tkinter/test/test_tkinter/test_font.py +++ b/Lib/tkinter/test/test_tkinter/test_font.py @@ -1,11 +1,8 @@ import unittest import tkinter from tkinter import font -from test.support import requires, run_unittest from tkinter.test.support import AbstractTkTest -requires('gui') - class FontTest(AbstractTkTest, unittest.TestCase): def test_font_eq(self): @@ -21,7 +18,5 @@ self.assertNotEqual(font1, font1.copy()) self.assertNotEqual(font1, 0) -tests_gui = (FontTest, ) - if __name__ == "__main__": - run_unittest(*tests_gui) + unittest.main() diff --git a/Lib/tkinter/test/test_tkinter/test_geometry_managers.py b/Lib/tkinter/test/test_tkinter/test_geometry_managers.py --- a/Lib/tkinter/test/test_tkinter/test_geometry_managers.py +++ b/Lib/tkinter/test/test_tkinter/test_geometry_managers.py @@ -2,13 +2,10 @@ import re import tkinter from tkinter import TclError -from test.support import requires from tkinter.test.support import pixels_conv, tcl_version, requires_tcl from tkinter.test.widget_tests import AbstractWidgetTest -requires('gui') - class PackTest(AbstractWidgetTest, unittest.TestCase): @@ -892,9 +889,5 @@ self.assertEqual(self.root.grid_slaves(row=1, column=1), [d, c]) -tests_gui = ( - PackTest, PlaceTest, GridTest, -) - if __name__ == '__main__': unittest.main() diff --git a/Lib/tkinter/test/test_tkinter/test_images.py b/Lib/tkinter/test/test_tkinter/test_images.py --- a/Lib/tkinter/test/test_tkinter/test_images.py +++ b/Lib/tkinter/test/test_tkinter/test_images.py @@ -3,9 +3,6 @@ from test import support from tkinter.test.support import AbstractTkTest, requires_tcl -support.requires('gui') - - class MiscTest(AbstractTkTest, unittest.TestCase): def test_image_types(self): @@ -321,7 +318,5 @@ self.assertEqual(image3.get(1, 2), image.get(5, 8)) -tests_gui = (MiscTest, BitmapImageTest, PhotoImageTest,) - if __name__ == "__main__": - support.run_unittest(*tests_gui) + unittest.main() diff --git a/Lib/tkinter/test/test_tkinter/test_loadtk.py b/Lib/tkinter/test/test_tkinter/test_loadtk.py --- a/Lib/tkinter/test/test_tkinter/test_loadtk.py +++ b/Lib/tkinter/test/test_tkinter/test_loadtk.py @@ -4,8 +4,8 @@ import test.support as test_support from tkinter import Tcl, TclError -test_support.requires('gui') +@test_support.requires_resource('gui') class TkLoadTest(unittest.TestCase): @unittest.skipIf('DISPLAY' not in os.environ, 'No $DISPLAY set.') @@ -16,14 +16,14 @@ self.assertEqual('1x1+0+0', tcl.winfo_geometry()) tcl.destroy() + @unittest.skipIf(sys.platform.startswith(('win', 'darwin', 'cygwin')), + "can't provoke a controlled failure on this platform") + # no failure possible on windows? + + # XXX Maybe on tk older than 8.4.13 it would be possible, + # see tkinter.h. def testLoadTkFailure(self): old_display = None - if sys.platform.startswith(('win', 'darwin', 'cygwin')): - # no failure possible on windows? - - # XXX Maybe on tk older than 8.4.13 it would be possible, - # see tkinter.h. - return with test_support.EnvironmentVarGuard() as env: if 'DISPLAY' in os.environ: del env['DISPLAY'] @@ -34,13 +34,11 @@ with os.popen('echo $DISPLAY') as pipe: display = pipe.read().strip() if display: - return + self.skipTest("couldn't make $DISPLAY disappear") tcl = Tcl() self.assertRaises(TclError, tcl.winfo_geometry) self.assertRaises(TclError, tcl.loadtk) -tests_gui = (TkLoadTest, ) - if __name__ == "__main__": - test_support.run_unittest(*tests_gui) + unittest.main() diff --git a/Lib/tkinter/test/test_tkinter/test_misc.py b/Lib/tkinter/test/test_tkinter/test_misc.py --- a/Lib/tkinter/test/test_tkinter/test_misc.py +++ b/Lib/tkinter/test/test_tkinter/test_misc.py @@ -3,8 +3,6 @@ from test import support from tkinter.test.support import AbstractTkTest -support.requires('gui') - class MiscTest(AbstractTkTest, unittest.TestCase): def test_repr(self): @@ -41,7 +39,5 @@ root.tk_setPalette, highlightColor='blue') -tests_gui = (MiscTest, ) - if __name__ == "__main__": - support.run_unittest(*tests_gui) + unittest.main() diff --git a/Lib/tkinter/test/test_tkinter/test_text.py b/Lib/tkinter/test/test_tkinter/test_text.py --- a/Lib/tkinter/test/test_tkinter/test_text.py +++ b/Lib/tkinter/test/test_tkinter/test_text.py @@ -1,9 +1,7 @@ import unittest import tkinter -from test.support import requires, run_unittest from tkinter.test.support import AbstractTkTest -requires('gui') class TextTest(AbstractTkTest, unittest.TestCase): @@ -41,7 +39,5 @@ self.assertEqual(text.search('test', '1.0', 'end'), '1.3') -tests_gui = (TextTest, ) - if __name__ == "__main__": - run_unittest(*tests_gui) + unittest.main() diff --git a/Lib/tkinter/test/test_tkinter/test_variables.py b/Lib/tkinter/test/test_tkinter/test_variables.py --- a/Lib/tkinter/test/test_tkinter/test_variables.py +++ b/Lib/tkinter/test/test_tkinter/test_variables.py @@ -177,10 +177,5 @@ v.get() -tests_gui = (TestVariable, TestStringVar, TestIntVar, - TestDoubleVar, TestBooleanVar) - - if __name__ == "__main__": - from test.support import run_unittest - run_unittest(*tests_gui) + unittest.main() diff --git a/Lib/tkinter/test/test_tkinter/test_widgets.py b/Lib/tkinter/test/test_tkinter/test_widgets.py --- a/Lib/tkinter/test/test_tkinter/test_widgets.py +++ b/Lib/tkinter/test/test_tkinter/test_widgets.py @@ -3,17 +3,14 @@ from tkinter import TclError import os import sys -from test.support import requires from tkinter.test.support import (tcl_version, requires_tcl, - get_tk_patchlevel, widget_eq) + get_tk_patchlevel, widget_eq) from tkinter.test.widget_tests import ( add_standard_options, noconv, pixels_round, AbstractWidgetTest, StandardOptionsTests, IntegerSizeTests, PixelSizeTests, setUpModule) -requires('gui') - def float_round(x): return float(round(x)) @@ -1182,13 +1179,5 @@ self.checkIntegerParam(widget, 'aspect', 250, 0, -300) -tests_gui = ( - ButtonTest, CanvasTest, CheckbuttonTest, EntryTest, - FrameTest, LabelFrameTest,LabelTest, ListboxTest, - MenubuttonTest, MenuTest, MessageTest, OptionMenuTest, - PanedWindowTest, RadiobuttonTest, ScaleTest, ScrollbarTest, - SpinboxTest, TextTest, ToplevelTest, -) - if __name__ == '__main__': unittest.main() diff --git a/Lib/tkinter/test/test_ttk/__init__.py b/Lib/tkinter/test/test_ttk/__init__.py --- a/Lib/tkinter/test/test_ttk/__init__.py +++ b/Lib/tkinter/test/test_ttk/__init__.py @@ -0,0 +1,5 @@ +import os +from test.support import load_package_tests + +def load_tests(*args): + return load_package_tests(os.path.dirname(__file__), *args) diff --git a/Lib/tkinter/test/test_ttk/__main__.py b/Lib/tkinter/test/test_ttk/__main__.py new file mode 100644 --- /dev/null +++ b/Lib/tkinter/test/test_ttk/__main__.py @@ -0,0 +1,4 @@ +from . import load_tests +import unittest + +unittest.main() diff --git a/Lib/tkinter/test/test_ttk/test_extensions.py b/Lib/tkinter/test/test_ttk/test_extensions.py --- a/Lib/tkinter/test/test_ttk/test_extensions.py +++ b/Lib/tkinter/test/test_ttk/test_extensions.py @@ -2,10 +2,13 @@ import unittest import tkinter from tkinter import ttk -from test.support import requires, run_unittest, swap_attr -from tkinter.test.support import AbstractTkTest, destroy_default_root +from test.support import swap_attr +from tkinter.test.support import ( + AbstractTkTest, check_ttk_availability, destroy_default_root, + ) -requires('gui') +def setUpModule(): + check_ttk_availability(__name__) class LabeledScaleTest(AbstractTkTest, unittest.TestCase): @@ -285,7 +288,5 @@ optmenu.destroy() -tests_gui = (LabeledScaleTest, OptionMenuTest) - if __name__ == "__main__": - run_unittest(*tests_gui) + unittest.main() diff --git a/Lib/tkinter/test/test_ttk/test_functions.py b/Lib/tkinter/test/test_ttk/test_functions.py --- a/Lib/tkinter/test/test_ttk/test_functions.py +++ b/Lib/tkinter/test/test_ttk/test_functions.py @@ -468,8 +468,5 @@ self.assertEqual(ttk.tclobjs_to_py({'text': 'some text'}), {'text': 'some text'}) -tests_nogui = (InternalFunctionsTest, TclObjsToPyTest) - if __name__ == "__main__": - from test.support import run_unittest - run_unittest(*tests_nogui) + unittest.main() diff --git a/Lib/tkinter/test/test_ttk/test_style.py b/Lib/tkinter/test/test_ttk/test_style.py --- a/Lib/tkinter/test/test_ttk/test_style.py +++ b/Lib/tkinter/test/test_ttk/test_style.py @@ -1,10 +1,10 @@ import unittest import tkinter from tkinter import ttk -from test.support import requires, run_unittest -from tkinter.test.support import AbstractTkTest +from tkinter.test.support import AbstractTkTest, check_ttk_availability -requires('gui') +def setUpModule(): + check_ttk_availability(__name__) class StyleTest(AbstractTkTest, unittest.TestCase): @@ -86,7 +86,5 @@ self.style.theme_use(curr_theme) -tests_gui = (StyleTest, ) - if __name__ == "__main__": - run_unittest(*tests_gui) + unittest.main() diff --git a/Lib/tkinter/test/test_ttk/test_widgets.py b/Lib/tkinter/test/test_ttk/test_widgets.py --- a/Lib/tkinter/test/test_ttk/test_widgets.py +++ b/Lib/tkinter/test/test_ttk/test_widgets.py @@ -1,18 +1,19 @@ import unittest import tkinter from tkinter import ttk -from test.support import requires import sys from tkinter.test.test_ttk.test_functions import MockTclObj -from tkinter.test.support import (AbstractTkTest, tcl_version, get_tk_patchlevel, - simulate_mouse_click) +from tkinter.test.support import (AbstractTkTest, tcl_version, + check_ttk_availability, get_tk_patchlevel, simulate_mouse_click, + ) from tkinter.test.widget_tests import (add_standard_options, noconv, AbstractWidgetTest, StandardOptionsTests, IntegerSizeTests, PixelSizeTests, - setUpModule) + setUpModule as _setUpWidgetTestModule) -requires('gui') - +def setUpModule(): + _setUpWidgetTestModule() + check_ttk_availability(__name__) class StandardTtkOptionsTests(StandardOptionsTests): @@ -1586,13 +1587,6 @@ def create(self, **kwargs): return ttk.Sizegrip(self.root, **kwargs) -tests_gui = ( - ButtonTest, CheckbuttonTest, ComboboxTest, EntryTest, - FrameTest, LabelFrameTest, LabelTest, MenubuttonTest, - NotebookTest, PanedWindowTest, ProgressbarTest, - RadiobuttonTest, ScaleTest, ScrollbarTest, SeparatorTest, - SizegripTest, TreeviewTest, WidgetTest, - ) if __name__ == "__main__": unittest.main()

AltStyle によって変換されたページ (->オリジナル) /