cpython: 6298895a52de (original) (raw)
Mercurial > cpython
changeset 91784:6298895a52de
Closes #22002: Merge with 3.4 [#22002]
line wrap: on
line diff
--- a/Doc/library/test.rst
+++ b/Doc/library/test.rst
@@ -461,7 +461,7 @@ The :mod:test.support
module defines t
.. function:: make_bad_fd()
Create an invalid file descriptor by opening and closing a temporary file,
.. function:: import_module(name, deprecated=False)
@@ -554,6 +554,21 @@ The :mod:test.support
module defines t
run simultaneously, which is a problem for buildbots.
+.. function:: load_package_tests(pkg_dir, loader, standard_tests, pattern)
+
- Generic implementation of the :mod:
unittest
load_tests
protocol for - use in test packages. pkg_dir is the root directory of the package;
- loader, standard_tests, and pattern are the arguments expected by
load_tests
. In simple cases, the test package's__init__.py
- can be the following:: +
import os[](#l1.24)
from test.support import load_package_tests[](#l1.25)
def load_tests(*args):[](#l1.27)
return load_package_tests(os.path.dirname(__file__), *args)[](#l1.28)
+
+
The :mod:test.support
module defines the following classes:
.. class:: TransientResource(exc, **kwargs)
--- a/Lib/test/support/init.py +++ b/Lib/test/support/init.py @@ -85,7 +85,7 @@ except ImportError: "skip_unless_symlink", "requires_gzip", "requires_bz2", "requires_lzma", "bigmemtest", "bigaddrspacetest", "cpython_only", "get_attribute", "requires_IEEE_754", "skip_unless_xattr", "requires_zlib",
@@ -188,6 +188,25 @@ def anticipate_failure(condition): return unittest.expectedFailure return lambda f: f +def load_package_tests(pkg_dir, loader, standard_tests, pattern):
def load_tests(*args):[](#l2.21)
return load_package_tests(os.path.dirname(__file__), *args)[](#l2.22)
- """
- if pattern is None:
pattern = "test*"[](#l2.25)
- top_dir = os.path.dirname( # Lib
os.path.dirname( # test[](#l2.27)
os.path.dirname(__file__))) # support[](#l2.28)
- package_tests = loader.discover(start_dir=pkg_dir,
top_level_dir=top_dir,[](#l2.30)
pattern=pattern)[](#l2.31)
- standard_tests.addTests(package_tests)
- return standard_tests
+ def import_fresh_module(name, fresh=(), blocked=(), deprecated=False): """Import and return a module, deliberately bypassing sys.modules.
--- a/Lib/test/test_asyncio/init.py +++ b/Lib/test/test_asyncio/init.py @@ -1,29 +1,10 @@ import os -import sys -import unittest -from test.support import run_unittest, import_module +from test.support import load_package_tests, import_module
Skip tests if we don't have threading.
Skip tests if we don't have concurrent.futures.
import_module('concurrent.futures') - -def suite():
- tests = unittest.TestSuite()
- loader = unittest.TestLoader()
- for fn in os.listdir(os.path.dirname(file)):
if fn.startswith("test") and fn.endswith(".py"):[](#l3.20)
mod_name = 'test.test_asyncio.' + fn[:-3][](#l3.21)
try:[](#l3.22)
__import__(mod_name)[](#l3.23)
except unittest.SkipTest:[](#l3.24)
pass[](#l3.25)
else:[](#l3.26)
mod = sys.modules[mod_name][](#l3.27)
tests.addTests(loader.loadTestsFromModule(mod))[](#l3.28)
- return tests
--- a/Lib/test/test_asyncio/main.py +++ b/Lib/test/test_asyncio/main.py @@ -1,5 +1,4 @@ -from . import test_main - +from . import load_tests +import unittest -if name == 'main':
--- a/Lib/test/test_email/init.py +++ b/Lib/test/test_email/init.py @@ -1,31 +1,16 @@ import os import sys import unittest -import test.support import collections import email from email.message import Message from email._policybase import compat32 +from test.support import load_package_tests from test.test_email import file as landmark -# Run all tests in package for '-m unittest test.test_email' -def load_tests(loader, standard_tests, pattern):
- this_dir = os.path.dirname(file)
- if pattern is None:
pattern = "test*"[](#l5.19)
- package_tests = loader.discover(start_dir=this_dir, pattern=pattern)
- standard_tests.addTests(package_tests)
- return standard_tests
- - -# used by regrtest and main. -def test_main():
- here = os.path.dirname(file)
Unittest mucks with the path, so we have to save and restore
it to keep regrtest happy.
- savepath = sys.path[:]
- test.support._run_suite(unittest.defaultTestLoader.discover(here))
- sys.path[:] = savepath
+# Load all tests in package +def load_tests(*args):
helper code used by a number of test modules.
--- a/Lib/test/test_email/main.py +++ b/Lib/test/test_email/main.py @@ -1,3 +1,4 @@ -from test.test_email import test_main +from test.test_email import load_tests +import unittest -test_main() +unittest.main()
--- a/Lib/test/test_importlib/init.py +++ b/Lib/test/test_importlib/init.py @@ -1,33 +1,5 @@ import os -import sys -from test import support -import unittest +from test.support import load_package_tests -def test_suite(package=package, directory=os.path.dirname(file)):
- suite = unittest.TestSuite()
- for name in os.listdir(directory):
if name.startswith(('.', '__')):[](#l7.13)
continue[](#l7.14)
path = os.path.join(directory, name)[](#l7.15)
if (os.path.isfile(path) and name.startswith('test_') and[](#l7.16)
name.endswith('.py')):[](#l7.17)
submodule_name = os.path.splitext(name)[0][](#l7.18)
module_name = "{0}.{1}".format(package, submodule_name)[](#l7.19)
__import__(module_name, level=0)[](#l7.20)
module_tests = unittest.findTestCases(sys.modules[module_name])[](#l7.21)
suite.addTest(module_tests)[](#l7.22)
elif os.path.isdir(path):[](#l7.23)
package_name = "{0}.{1}".format(package, name)[](#l7.24)
__import__(package_name, level=0)[](#l7.25)
package_tests = getattr(sys.modules[package_name], 'test_suite')()[](#l7.26)
suite.addTest(package_tests)[](#l7.27)
else:[](#l7.28)
continue[](#l7.29)
- return suite
- start_dir = os.path.dirname(file)
- top_dir = os.path.dirname(os.path.dirname(start_dir))
- test_loader = unittest.TestLoader()
- support.run_unittest(test_loader.discover(start_dir, top_level_dir=top_dir))
--- a/Lib/test/test_importlib/main.py
+++ b/Lib/test/test_importlib/main.py
@@ -1,9 +1,4 @@
-"""Run importlib's test suite.
-
-Specifying the --builtin
flag will run tests, where applicable, with
-builtins.import instead of importlib.import.
+from . import load_tests
+import unittest
-"""
-if name == 'main':
--- a/Lib/test/test_importlib/builtin/init.py +++ b/Lib/test/test_importlib/builtin/init.py @@ -1,12 +1,5 @@ -from .. import test_suite import os - +from test.support import load_package_tests -def test_suite():
new file mode 100644 --- /dev/null +++ b/Lib/test/test_importlib/builtin/main.py @@ -0,0 +1,4 @@ +from . import load_tests +import unittest + +unittest.main()
--- a/Lib/test/test_importlib/extension/init.py +++ b/Lib/test/test_importlib/extension/init.py @@ -1,13 +1,5 @@ -from .. import test_suite -import os.path -import unittest - +import os +from test.support import load_package_tests -def test_suite():
new file mode 100644 --- /dev/null +++ b/Lib/test/test_importlib/extension/main.py @@ -0,0 +1,4 @@ +from . import load_tests +import unittest + +unittest.main()
--- a/Lib/test/test_importlib/frozen/init.py +++ b/Lib/test/test_importlib/frozen/init.py @@ -1,13 +1,5 @@ -from .. import test_suite -import os.path -import unittest - +import os +from test.support import load_package_tests -def test_suite():
new file mode 100644 --- /dev/null +++ b/Lib/test/test_importlib/frozen/main.py @@ -0,0 +1,4 @@ +from . import load_tests +import unittest + +unittest.main()
--- a/Lib/test/test_importlib/import_/init.py +++ b/Lib/test/test_importlib/import_/init.py @@ -1,13 +1,5 @@ -from .. import test_suite -import os.path -import unittest - +import os +from test.support import load_package_tests -def test_suite():
new file mode 100644 --- /dev/null +++ b/Lib/test/test_importlib/import_/main.py @@ -0,0 +1,4 @@ +from . import load_tests +import unittest + +unittest.main()
--- a/Lib/test/test_importlib/source/init.py +++ b/Lib/test/test_importlib/source/init.py @@ -1,13 +1,5 @@ -from .. import test_suite -import os.path -import unittest - +import os +from test.support import load_package_tests -def test_suite():
new file mode 100644 --- /dev/null +++ b/Lib/test/test_importlib/source/main.py @@ -0,0 +1,4 @@ +from . import load_tests +import unittest + +unittest.main()
--- a/Lib/test/test_json/init.py +++ b/Lib/test/test_json/init.py @@ -42,23 +42,12 @@ class TestCTest(CTest): '_json') -here = os.path.dirname(file) - -def load_tests(*args):
- suite = additional_tests()
- loader = unittest.TestLoader()
- for fn in os.listdir(here):
if fn.startswith("test") and fn.endswith(".py"):[](#l19.13)
modname = "test.test_json." + fn[:-3][](#l19.14)
__import__(modname)[](#l19.15)
module = sys.modules[modname][](#l19.16)
suite.addTests(loader.loadTestsFromModule(module))[](#l19.17)
- return suite
- -def additional_tests(): +def load_tests(loader, _, pattern): suite = unittest.TestSuite() for mod in (json, json.encoder, json.decoder): suite.addTest(doctest.DocTestSuite(mod)) suite.addTest(TestPyTest('test_pyjson')) suite.addTest(TestCTest('test_cjson'))
--- a/Lib/test/test_tools/init.py +++ b/Lib/test/test_tools/init.py @@ -21,11 +21,5 @@ def import_tool(toolname): with support.DirsOnSysPath(scriptsdir): return importlib.import_module(toolname) -def load_tests(loader, standard_tests, pattern):
- this_dir = os.path.dirname(file)
- if pattern is None:
pattern = "test*"[](#l20.10)
- with support.DirsOnSysPath():
package_tests = loader.discover(start_dir=this_dir, pattern=pattern)[](#l20.12)
- standard_tests.addTests(package_tests)
- return standard_tests
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -716,6 +716,10 @@ Documentation
Tests
-----
+- Issue #22002: Added load_package_tests
function to test.support and used