Merge pull request #822 from jdufresne/decorate-all

Simplify 'decorate_all_tests' usage by decorating the test class
This commit is contained in:
Daniele Varrazzo 2018-12-04 14:22:02 +00:00 committed by GitHub
commit 68bacbb194
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 88 deletions

View File

@ -25,23 +25,14 @@ import unittest
import psycopg2 import psycopg2
import psycopg2.extras import psycopg2.extras
try:
@testutils.decorate_all_tests import ipaddress
def skip_if_no_ipaddress(f): except ImportError:
@wraps(f) # Python 2
def skip_if_no_ipaddress_(self): ipaddress = None
if sys.version_info[:2] < (3, 3):
try:
import ipaddress # noqa
except ImportError:
return self.skipTest("'ipaddress' module not available")
return f(self)
return skip_if_no_ipaddress_
@skip_if_no_ipaddress @unittest.skipIf(ipaddress is None, "'ipaddress' module not available")
class NetworkingTestCase(testutils.ConnectingTestCase): class NetworkingTestCase(testutils.ConnectingTestCase):
def test_inet_cast(self): def test_inet_cast(self):
import ipaddress as ip import ipaddress as ip

View File

@ -298,17 +298,13 @@ def skip_before_libpq(*ver):
"""Skip a test if libpq we're linked to is older than a certain version.""" """Skip a test if libpq we're linked to is older than a certain version."""
ver = ver + (0,) * (3 - len(ver)) ver = ver + (0,) * (3 - len(ver))
@decorate_all_tests def skip_before_libpq_(cls):
def skip_before_libpq_(f):
@wraps(f)
def skip_before_libpq__(self):
v = libpq_version() v = libpq_version()
if v < int("%d%02d%02d" % ver): decorator = unittest.skipIf(
return self.skipTest("skipped because libpq %d" % v) v < int("%d%02d%02d" % ver),
else: "skipped because libpq %d" % v,
return f(self) )
return decorator(cls)
return skip_before_libpq__
return skip_before_libpq_ return skip_before_libpq_
@ -316,49 +312,37 @@ def skip_after_libpq(*ver):
"""Skip a test if libpq we're linked to is newer than a certain version.""" """Skip a test if libpq we're linked to is newer than a certain version."""
ver = ver + (0,) * (3 - len(ver)) ver = ver + (0,) * (3 - len(ver))
@decorate_all_tests def skip_after_libpq_(cls):
def skip_after_libpq_(f):
@wraps(f)
def skip_after_libpq__(self):
v = libpq_version() v = libpq_version()
if v >= int("%d%02d%02d" % ver): decorator = unittest.skipIf(
return self.skipTest("skipped because libpq %s" % v) v >= int("%d%02d%02d" % ver),
else: "skipped because libpq %s" % v,
return f(self) )
return decorator(cls)
return skip_after_libpq__
return skip_after_libpq_ return skip_after_libpq_
def skip_before_python(*ver): def skip_before_python(*ver):
"""Skip a test on Python before a certain version.""" """Skip a test on Python before a certain version."""
@decorate_all_tests def skip_before_python_(cls):
def skip_before_python_(f): decorator = unittest.skipIf(
@wraps(f) sys.version_info[:len(ver)] < ver,
def skip_before_python__(self): "skipped because Python %s"
if sys.version_info[:len(ver)] < ver: % ".".join(map(str, sys.version_info[:len(ver)])),
return self.skipTest("skipped because Python %s" )
% ".".join(map(str, sys.version_info[:len(ver)]))) return decorator(cls)
else:
return f(self)
return skip_before_python__
return skip_before_python_ return skip_before_python_
def skip_from_python(*ver): def skip_from_python(*ver):
"""Skip a test on Python after (including) a certain version.""" """Skip a test on Python after (including) a certain version."""
@decorate_all_tests def skip_from_python_(cls):
def skip_from_python_(f): decorator = unittest.skipIf(
@wraps(f) sys.version_info[:len(ver)] >= ver,
def skip_from_python__(self): "skipped because Python %s"
if sys.version_info[:len(ver)] >= ver: % ".".join(map(str, sys.version_info[:len(ver)])),
return self.skipTest("skipped because Python %s" )
% ".".join(map(str, sys.version_info[:len(ver)]))) return decorator(cls)
else:
return f(self)
return skip_from_python__
return skip_from_python_ return skip_from_python_
@ -381,44 +365,31 @@ def skip_if_no_superuser(f):
def skip_if_green(reason): def skip_if_green(reason):
@decorate_all_tests def skip_if_green_(cls):
def skip_if_green_(f):
@wraps(f)
def skip_if_green__(self):
from .testconfig import green from .testconfig import green
if green: decorator = unittest.skipIf(green, reason)
return self.skipTest(reason) return decorator(cls)
else:
return f(self)
return skip_if_green__
return skip_if_green_ return skip_if_green_
skip_copy_if_green = skip_if_green("copy in async mode currently not supported") skip_copy_if_green = skip_if_green("copy in async mode currently not supported")
@decorate_all_tests def skip_if_no_getrefcount(cls):
def skip_if_no_getrefcount(f): decorator = unittest.skipUnless(
@wraps(f) hasattr(sys, 'getrefcount'),
def skip_if_no_getrefcount_(self): 'no sys.getrefcount()',
if not hasattr(sys, 'getrefcount'): )
return self.skipTest('skipped, no sys.getrefcount()') return decorator(cls)
else:
return f(self)
return skip_if_no_getrefcount_
@decorate_all_tests def skip_if_windows(cls):
def skip_if_windows(f):
"""Skip a test if run on windows""" """Skip a test if run on windows"""
@wraps(f) decorator = unittest.skipIf(
def skip_if_windows_(self): platform.system() == 'Windows',
if platform.system() == 'Windows': "Not supported on Windows",
return self.skipTest("Not supported on Windows") )
else: return decorator(cls)
return f(self)
return skip_if_windows_
class py3_raises_typeerror(object): class py3_raises_typeerror(object):