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/tkinter/test/README b/Lib/test/test_tkinter/README rename from Lib/tkinter/test/README rename to Lib/test/test_tkinter/README diff --git a/Lib/tkinter/test/__init__.py b/Lib/test/test_tkinter/__init__.py rename from Lib/tkinter/test/__init__.py rename to Lib/test/test_tkinter/__init__.py --- a/Lib/tkinter/test/__init__.py +++ b/Lib/test/test_tkinter/__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/test/test_tkinter/__main__.py b/Lib/test/test_tkinter/__main__.py new file mode 100644 --- /dev/null +++ b/Lib/test/test_tkinter/__main__.py @@ -0,0 +1,4 @@ +from . import load_tests +import unittest + +unittest.main() diff --git a/Lib/tkinter/test/support.py b/Lib/test/test_tkinter/support.py rename from Lib/tkinter/test/support.py rename to Lib/test/test_tkinter/support.py --- a/Lib/tkinter/test/support.py +++ b/Lib/test/test_tkinter/support.py @@ -1,10 +1,11 @@ import sys import tkinter +from tkinter import ttk import unittest -from test.support import requires +import test.support def get_tk_root(): - requires('gui') # raise exception if tk unavailable + test.support.requires('gui') # raise exception if tk unavailable try: root = tkinter._default_root except AttributeError: @@ -89,3 +90,33 @@ if isinstance(expected, (str, tkinter.Widget)): return str(actual) == str(expected) return False + +def setUpGUIModule(name=None): + if name is not None: + msg = '{} requires the "gui" resource'.format(name) + else: + msg = None + test.support.requires('gui', msg) + if test.support.verbose: + tcl = tkinter.Tcl() + print('patchlevel =', tcl.call('info', 'patchlevel')) + +def check_ttk_availability(): + # Skip if we can't create a ttk Button + root = None + try: + root = get_tk_root() + btn = ttk.Button(root) + except unittest.SkipTest: + # prefer a skip raised elsewhere + raise + 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() diff --git a/Lib/test/test_tcl.py b/Lib/test/test_tkinter/test_tcl.py rename from Lib/test/test_tcl.py rename to Lib/test/test_tkinter/test_tcl.py --- a/Lib/test/test_tcl.py +++ b/Lib/test/test_tkinter/test_tcl.py @@ -3,12 +3,10 @@ import os from test import support -# Skip this test if the _tkinter module wasn't built. -_tkinter = support.import_module('_tkinter') +from test.test_tkinter.support import get_tk_patchlevel -# Make sure tkinter._fix runs to set up the environment -support.import_fresh_module('tkinter') - +import tkinter +import _tkinter from tkinter import Tcl from _tkinter import TclError @@ -25,21 +23,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): @@ -606,9 +589,5 @@ tcl = Tcl() 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/test/test_tkinter/test_tk/__init__.py rename from Lib/tkinter/test/test_tkinter/__init__.py rename to Lib/test/test_tkinter/test_tk/__init__.py --- a/Lib/tkinter/test/test_tkinter/__init__.py +++ b/Lib/test/test_tkinter/test_tk/__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/test/test_tkinter/test_tk/__main__.py b/Lib/test/test_tkinter/test_tk/__main__.py new file mode 100644 --- /dev/null +++ b/Lib/test/test_tkinter/test_tk/__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/test/test_tkinter/test_tk/test_font.py rename from Lib/tkinter/test/test_tkinter/test_font.py rename to Lib/test/test_tkinter/test_tk/test_font.py --- a/Lib/tkinter/test/test_tkinter/test_font.py +++ b/Lib/test/test_tkinter/test_tk/test_font.py @@ -1,10 +1,10 @@ import unittest import tkinter from tkinter import font -from test.support import requires, run_unittest -import tkinter.test.support as support +from test.test_tkinter import support -requires('gui') +def setUpModule(): + support.setUpGUIModule(__name__) class FontTest(unittest.TestCase): @@ -27,7 +27,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/test/test_tkinter/test_tk/test_geometry_managers.py rename from Lib/tkinter/test/test_tkinter/test_geometry_managers.py rename to Lib/test/test_tkinter/test_tk/test_geometry_managers.py --- a/Lib/tkinter/test/test_tkinter/test_geometry_managers.py +++ b/Lib/test/test_tkinter/test_tk/test_geometry_managers.py @@ -2,12 +2,13 @@ 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 +from test.test_tkinter.support import (pixels_conv, tcl_version, + requires_tcl, setUpGUIModule) +from test.test_tkinter.widget_tests import AbstractWidgetTest -requires('gui') +def setUpModule(): + setUpGUIModule(__name__) class PackTest(AbstractWidgetTest, unittest.TestCase): @@ -893,10 +894,5 @@ self.assertEqual(self.root.grid_slaves(column=1), [d, c, a]) 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/test/test_tkinter/test_tk/test_images.py rename from Lib/tkinter/test/test_tkinter/test_images.py rename to Lib/test/test_tkinter/test_tk/test_images.py --- a/Lib/tkinter/test/test_tkinter/test_images.py +++ b/Lib/test/test_tkinter/test_tk/test_images.py @@ -2,10 +2,11 @@ import tkinter from tkinter import ttk from test import support -from tkinter.test.support import requires_tcl -support.requires('gui') +from test.test_tkinter.support import requires_tcl, setUpGUIModule +def setUpModule(): + setUpGUIModule(__name__) class MiscTest(unittest.TestCase): @@ -330,7 +331,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/test/test_tkinter/test_tk/test_loadtk.py rename from Lib/tkinter/test/test_tkinter/test_loadtk.py rename to Lib/test/test_tkinter/test_tk/test_loadtk.py --- a/Lib/tkinter/test/test_tkinter/test_loadtk.py +++ b/Lib/test/test_tkinter/test_tk/test_loadtk.py @@ -4,7 +4,10 @@ import test.support as test_support from tkinter import Tcl, TclError -test_support.requires('gui') +from test.test_tkinter.support import setUpGUIModule + +def setUpModule(): + setUpGUIModule(__name__) class TkLoadTest(unittest.TestCase): @@ -40,7 +43,5 @@ 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/test/test_tkinter/test_tk/test_misc.py rename from Lib/tkinter/test/test_tkinter/test_misc.py rename to Lib/test/test_tkinter/test_tk/test_misc.py --- a/Lib/tkinter/test/test_tkinter/test_misc.py +++ b/Lib/test/test_tkinter/test_tk/test_misc.py @@ -1,9 +1,11 @@ import unittest import tkinter from tkinter import ttk -from test import support -support.requires('gui') +from test.test_tkinter.support import setUpGUIModule + +def setUpModule(): + setUpGUIModule(__name__) class MiscTest(unittest.TestCase): @@ -43,8 +45,5 @@ '^must specify a background color$', 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/test/test_tkinter/test_tk/test_text.py rename from Lib/tkinter/test/test_tkinter/test_text.py rename to Lib/test/test_tkinter/test_tk/test_text.py --- a/Lib/tkinter/test/test_tkinter/test_text.py +++ b/Lib/test/test_tkinter/test_tk/test_text.py @@ -1,9 +1,11 @@ import unittest import tkinter -from test.support import requires, run_unittest from tkinter.ttk import setup_master -requires('gui') +from test.test_tkinter.support import setUpGUIModule + +def setUpModule(): + setUpGUIModule(__name__) class TextTest(unittest.TestCase): @@ -43,8 +45,5 @@ self.assertEqual(text.search('-test', '1.0', 'end'), '1.2') 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/test/test_tkinter/test_tk/test_variables.py rename from Lib/tkinter/test/test_tkinter/test_variables.py rename to Lib/test/test_tkinter/test_tk/test_variables.py --- a/Lib/tkinter/test/test_tkinter/test_variables.py +++ b/Lib/test/test_tkinter/test_tk/test_variables.py @@ -2,6 +2,10 @@ from tkinter import Variable, StringVar, IntVar, DoubleVar, BooleanVar, Tk +from test.test_tkinter.support import setUpGUIModule + +def setUpModule(): + setUpGUIModule(__name__) class Var(Variable): @@ -177,10 +181,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/test/test_tkinter/test_tk/test_widgets.py rename from Lib/tkinter/test/test_tkinter/test_widgets.py rename to Lib/test/test_tkinter/test_tk/test_widgets.py --- a/Lib/tkinter/test/test_tkinter/test_widgets.py +++ b/Lib/test/test_tkinter/test_tk/test_widgets.py @@ -3,17 +3,17 @@ 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) -from tkinter.test.widget_tests import ( +from test.test_tkinter.support import (tcl_version, requires_tcl, + get_tk_patchlevel, widget_eq, + setUpGUIModule) +from test.test_tkinter.widget_tests import ( add_standard_options, noconv, pixels_round, AbstractWidgetTest, StandardOptionsTests, IntegerSizeTests, PixelSizeTests, - setUpModule) + ) -requires('gui') - +def setUpModule(): + setUpGUIModule(__name__) def float_round(x): return float(round(x)) @@ -1182,13 +1182,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/test/test_tkinter/test_ttk/__init__.py rename from Lib/tkinter/test/test_ttk/__init__.py rename to Lib/test/test_tkinter/test_ttk/__init__.py --- a/Lib/tkinter/test/test_ttk/__init__.py +++ b/Lib/test/test_tkinter/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/test/test_tkinter/test_ttk/__main__.py b/Lib/test/test_tkinter/test_ttk/__main__.py new file mode 100644 --- /dev/null +++ b/Lib/test/test_tkinter/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/test/test_tkinter/test_ttk/test_extensions.py rename from Lib/tkinter/test/test_ttk/test_extensions.py rename to Lib/test/test_tkinter/test_ttk/test_extensions.py --- a/Lib/tkinter/test/test_ttk/test_extensions.py +++ b/Lib/test/test_tkinter/test_ttk/test_extensions.py @@ -2,11 +2,12 @@ import unittest import tkinter from tkinter import ttk -from test.support import requires, run_unittest -import tkinter.test.support as support +from test.test_tkinter import support -requires('gui') +def setUpModule(): + support.setUpGUIModule(__name__) + support.check_ttk_availability() class LabeledScaleTest(unittest.TestCase): @@ -278,7 +279,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/test/test_tkinter/test_ttk/test_functions.py rename from Lib/tkinter/test/test_ttk/test_functions.py rename to Lib/test/test_tkinter/test_ttk/test_functions.py --- a/Lib/tkinter/test/test_ttk/test_functions.py +++ b/Lib/test/test_tkinter/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/test/test_tkinter/test_ttk/test_style.py rename from Lib/tkinter/test/test_ttk/test_style.py rename to Lib/test/test_tkinter/test_ttk/test_style.py --- a/Lib/tkinter/test/test_ttk/test_style.py +++ b/Lib/test/test_tkinter/test_ttk/test_style.py @@ -1,11 +1,12 @@ import unittest import tkinter from tkinter import ttk -from test.support import requires, run_unittest -import tkinter.test.support as support +from test.test_tkinter import support -requires('gui') +def setUpModule(): + support.setUpGUIModule(__name__) + support.check_ttk_availability() class StyleTest(unittest.TestCase): @@ -86,7 +87,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/test/test_tkinter/test_ttk/test_widgets.py rename from Lib/tkinter/test/test_ttk/test_widgets.py rename to Lib/test/test_tkinter/test_ttk/test_widgets.py --- a/Lib/tkinter/test/test_ttk/test_widgets.py +++ b/Lib/test/test_tkinter/test_ttk/test_widgets.py @@ -1,18 +1,18 @@ import unittest import tkinter from tkinter import ttk -from test.support import requires import sys -import tkinter.test.support as support -from tkinter.test.test_ttk.test_functions import MockTclObj -from tkinter.test.support import tcl_version, get_tk_patchlevel -from tkinter.test.widget_tests import (add_standard_options, noconv, +from test.test_tkinter import support +from test.test_tkinter.test_ttk.test_functions import MockTclObj +from test.test_tkinter.support import tcl_version, get_tk_patchlevel +from test.test_tkinter.widget_tests import (add_standard_options, noconv, AbstractWidgetTest, StandardOptionsTests, IntegerSizeTests, PixelSizeTests, - setUpModule) + ) -requires('gui') - +def setUpModule(): + support.setUpGUIModule(__name__) + support.check_ttk_availability() class StandardTtkOptionsTests(StandardOptionsTests): @@ -1626,13 +1626,5 @@ 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() diff --git a/Lib/tkinter/test/widget_tests.py b/Lib/test/test_tkinter/widget_tests.py rename from Lib/tkinter/test/widget_tests.py rename to Lib/test/test_tkinter/widget_tests.py --- a/Lib/tkinter/test/widget_tests.py +++ b/Lib/test/test_tkinter/widget_tests.py @@ -4,8 +4,9 @@ import sys import tkinter from tkinter.ttk import setup_master, Scale -from tkinter.test.support import (tcl_version, requires_tcl, get_tk_patchlevel, - pixels_conv, tcl_obj_eq) +from test.test_tkinter.support import (tcl_version, requires_tcl, + get_tk_patchlevel, pixels_conv, + tcl_obj_eq) import test.support @@ -521,8 +522,3 @@ setattr(cls, methodname, test) return cls return decorator - -def setUpModule(): - if test.support.verbose: - tcl = tkinter.Tcl() - print('patchlevel =', tcl.call('info', 'patchlevel')) 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,33 +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 -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 -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(): - try: - support.run_unittest( - *runtktests.get_tests(text=False, packages=['test_ttk'])) - finally: - get_tk_root().destroy() - -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/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())