summaryrefslogtreecommitdiffstats
path: root/Lib/test/support.py
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2009-03-28 21:42:05 (GMT)
committerBenjamin Peterson <benjamin@python.org>2009-03-28 21:42:05 (GMT)
commite549ead8263819ac47f60cdd0239592750888f0b (patch)
tree7e1199b4b197ac5facd66b03cbb7e768fa2b6892 /Lib/test/support.py
parentb556452055590c6d8f658d90c6be00aec31f5620 (diff)
downloadcpython-e549ead8263819ac47f60cdd0239592750888f0b.zip
cpython-e549ead8263819ac47f60cdd0239592750888f0b.tar.gz
cpython-e549ead8263819ac47f60cdd0239592750888f0b.tar.bz2
Merged revisions 70554,70588-70589,70598,70605,70611-70621,70623-70624,70626-70627 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r70554 | benjamin.peterson | 2009-03-23 16:25:15 -0500 (Mon, 23 Mar 2009) | 1 line complain when there's no last exception ........ r70588 | benjamin.peterson | 2009-03-24 17:56:32 -0500 (Tue, 24 Mar 2009) | 1 line fix newline issue in test summary ........ r70589 | benjamin.peterson | 2009-03-24 18:07:07 -0500 (Tue, 24 Mar 2009) | 1 line another style nit ........ r70598 | benjamin.peterson | 2009-03-25 16:24:04 -0500 (Wed, 25 Mar 2009) | 1 line add shorthands for expected failures and unexpected success ........ r70605 | benjamin.peterson | 2009-03-26 11:32:23 -0500 (Thu, 26 Mar 2009) | 1 line remove uneeded function ........ r70611 | benjamin.peterson | 2009-03-26 13:35:37 -0500 (Thu, 26 Mar 2009) | 1 line add much better tests for python version information parsing ........ r70612 | benjamin.peterson | 2009-03-26 13:55:48 -0500 (Thu, 26 Mar 2009) | 1 line more and more implementations now support sys.subversion ........ r70613 | benjamin.peterson | 2009-03-26 13:58:30 -0500 (Thu, 26 Mar 2009) | 1 line roll old test in with new one ........ r70614 | benjamin.peterson | 2009-03-26 14:09:21 -0500 (Thu, 26 Mar 2009) | 1 line add support for PyPy ........ r70615 | benjamin.peterson | 2009-03-26 14:58:18 -0500 (Thu, 26 Mar 2009) | 5 lines add some useful utilities for skipping tests with unittest's new skipping ability most significantly apply a modified portion of the patch from #4242 with patches for skipping implementation details ........ r70616 | benjamin.peterson | 2009-03-26 15:05:50 -0500 (Thu, 26 Mar 2009) | 1 line rename TestCase.skip() to skipTest() because it causes annoying problems with trial #5571 ........ r70617 | benjamin.peterson | 2009-03-26 15:17:27 -0500 (Thu, 26 Mar 2009) | 1 line apply the second part of #4242's patch; classify all the implementation details in test_descr ........ r70618 | benjamin.peterson | 2009-03-26 15:48:25 -0500 (Thu, 26 Mar 2009) | 1 line remove test_support.TestSkipped and just use unittest.SkipTest ........ r70619 | benjamin.peterson | 2009-03-26 15:49:40 -0500 (Thu, 26 Mar 2009) | 1 line fix naming ........ r70620 | benjamin.peterson | 2009-03-26 16:10:30 -0500 (Thu, 26 Mar 2009) | 1 line fix incorrect auto-translation of TestSkipped -> unittest.SkipTest ........ r70621 | benjamin.peterson | 2009-03-26 16:11:16 -0500 (Thu, 26 Mar 2009) | 1 line must pass argument to get expected behavior ;) ........ r70623 | benjamin.peterson | 2009-03-26 16:30:10 -0500 (Thu, 26 Mar 2009) | 1 line add missing import ........ r70624 | benjamin.peterson | 2009-03-26 16:30:54 -0500 (Thu, 26 Mar 2009) | 1 line ** is required here ........ r70626 | benjamin.peterson | 2009-03-26 16:40:29 -0500 (Thu, 26 Mar 2009) | 1 line update email tests to use SkipTest ........ r70627 | benjamin.peterson | 2009-03-26 16:44:43 -0500 (Thu, 26 Mar 2009) | 1 line fix another name ........
Diffstat (limited to 'Lib/test/support.py')
-rw-r--r--Lib/test/support.py96
1 files changed, 73 insertions, 23 deletions
diff --git a/Lib/test/support.py b/Lib/test/support.py
index 0fa9d6b..e209170 100644
--- a/Lib/test/support.py
+++ b/Lib/test/support.py
@@ -8,12 +8,12 @@ import errno
import socket
import sys
import os
-import os.path
+import platform
import shutil
import warnings
import unittest
-__all__ = ["Error", "TestFailed", "TestSkipped", "ResourceDenied", "import_module",
+__all__ = ["Error", "TestFailed", "ResourceDenied", "import_module",
"verbose", "use_resources", "max_memuse", "record_original_stdout",
"get_original_stdout", "unload", "unlink", "rmtree", "forget",
"is_resource_enabled", "requires", "find_unused_port", "bind_port",
@@ -24,7 +24,7 @@ __all__ = ["Error", "TestFailed", "TestSkipped", "ResourceDenied", "import_modul
"TransientResource", "transient_internet", "run_with_locale",
"set_memlimit", "bigmemtest", "bigaddrspacetest", "BasicTestRunner",
"run_unittest", "run_doctest", "threading_setup", "threading_cleanup",
- "reap_children"]
+ "reap_children", "cpython_only", "check_impl_detail"]
class Error(Exception):
"""Base class for regression test exceptions."""
@@ -32,17 +32,7 @@ class Error(Exception):
class TestFailed(Error):
"""Test failed."""
-class TestSkipped(Error):
- """Test skipped.
-
- This can be raised to indicate that a test was deliberatly
- skipped, but not because a feature wasn't available. For
- example, if some resource can't be used, such as the network
- appears to be unavailable, this should be raised instead of
- TestFailed.
- """
-
-class ResourceDenied(TestSkipped):
+class ResourceDenied(unittest.SkipTest):
"""Test skipped because it requested a disallowed resource.
This is raised when a test calls requires() for a resource that
@@ -51,7 +41,7 @@ class ResourceDenied(TestSkipped):
"""
def import_module(name, deprecated=False):
- """Import the module to be tested, raising TestSkipped if it is not
+ """Import the module to be tested, raising SkipTest if it is not
available."""
with warnings.catch_warnings():
if deprecated:
@@ -60,7 +50,7 @@ def import_module(name, deprecated=False):
try:
module = __import__(name, level=0)
except ImportError:
- raise TestSkipped("No module named " + name)
+ raise unittest.SkipTest("No module named " + name)
else:
return module
@@ -124,7 +114,7 @@ def requires(resource, msg=None):
possibility of False being returned occurs when regrtest.py is executing."""
# see if the caller's module is __main__ - if so, treat as if
# the resource was set
- if sys._getframe().f_back.f_globals.get("__name__") == "__main__":
+ if sys._getframe(1).f_globals.get("__name__") == "__main__":
return
if not is_resource_enabled(resource):
if msg is None:
@@ -357,12 +347,8 @@ def make_bad_fd():
unlink(TESTFN)
def check_syntax_error(testcase, statement):
- try:
- compile(statement, '<test string>', 'exec')
- except SyntaxError:
- pass
- else:
- testcase.fail('Missing SyntaxError: "%s"' % statement)
+ testcase.assertRaises(SyntaxError, compile, statement,
+ '<test string>', 'exec')
def open_urlresource(url, *args, **kw):
import urllib.request, urllib.parse
@@ -522,6 +508,21 @@ def captured_output(stream_name):
def captured_stdout():
return captured_output("stdout")
+def gc_collect():
+ """Force as many objects as possible to be collected.
+
+ In non-CPython implementations of Python, this is needed because timely
+ deallocation is not guaranteed by the garbage collector. (Even in CPython
+ this can be the case in case of reference cycles.) This means that __del__
+ methods may be called later than expected and weakrefs may remain alive for
+ longer than expected. This function tries its best to force all garbage
+ objects to disappear.
+ """
+ import gc
+ gc.collect()
+ gc.collect()
+ gc.collect()
+
#=======================================================================
# Decorator for running a function in a different locale, correctly resetting
@@ -681,6 +682,55 @@ class BasicTestRunner:
test(result)
return result
+def _id(obj):
+ return obj
+
+def requires_resource(resource):
+ if resource_is_enabled(resource):
+ return _id
+ else:
+ return unittest.skip("resource {0!r} is not enabled".format(resource))
+
+def cpython_only(test):
+ """
+ Decorator for tests only applicable on CPython.
+ """
+ return impl_detail(cpython=True)(test)
+
+def impl_detail(msg=None, **guards):
+ if check_impl_detail(**guards):
+ return _id
+ if msg is None:
+ guardnames, default = _parse_guards(guards)
+ if default:
+ msg = "implementation detail not available on {0}"
+ else:
+ msg = "implementation detail specific to {0}"
+ guardnames = sorted(guardnames.keys())
+ msg = msg.format(' or '.join(guardnames))
+ return unittest.skip(msg)
+
+def _parse_guards(guards):
+ # Returns a tuple ({platform_name: run_me}, default_value)
+ if not guards:
+ return ({'cpython': True}, False)
+ is_true = guards.values()[0]
+ assert guards.values() == [is_true] * len(guards) # all True or all False
+ return (guards, not is_true)
+
+# Use the following check to guard CPython's implementation-specific tests --
+# or to run them only on the implementation(s) guarded by the arguments.
+def check_impl_detail(**guards):
+ """This function returns True or False depending on the host platform.
+ Examples:
+ if check_impl_detail(): # only on CPython (default)
+ if check_impl_detail(jython=True): # only on Jython
+ if check_impl_detail(cpython=False): # everywhere except on CPython
+ """
+ guards, default = _parse_guards(guards)
+ return guards.get(platform.python_implementation().lower(), default)
+
+
def _run_suite(suite):
"""Run tests from a unittest.TestSuite-derived class."""