Commit c65eb4ef authored by Vincent Pelletier's avatar Vincent Pelletier

tests: Reuse unittest's functions & classes when present.

Fixes unittest.expectedFailure usage, allowing phasing out
backportUnittest gradually (at least for the backport part).
parent 3f8b6a39
...@@ -9,31 +9,87 @@ import unittest ...@@ -9,31 +9,87 @@ import unittest
import sys import sys
import time import time
class SkipTest(Exception): try:
""" SkipTest = unittest.SkipTest
Raise this exception in a test to skip it. _ExpectedFailure = unittest.case._ExpectedFailure
_UnexpectedSuccess = unittest.case._UnexpectedSuccess
Usually you can use TestResult.skip() or one of the skipping decorators skip = unittest.skip
instead of raising this directly. skipIf = unittest.skipIf
""" skipUnless = unittest.skipUnless
pass expectedFailure = unittest.expectedFailure
except AttributeError:
class _ExpectedFailure(Exception): class SkipTest(Exception):
""" """
Raise this when a test is expected to fail. Raise this exception in a test to skip it.
This is an implementation detail. Usually you can use TestResult.skip() or one of the skipping decorators
""" instead of raising this directly.
"""
def __init__(self, exc_info): pass
Exception.__init__(self)
self.exc_info = exc_info class _ExpectedFailure(Exception):
"""
class _UnexpectedSuccess(Exception): Raise this when a test is expected to fail.
"""
The test was supposed to fail, but it didn't! This is an implementation detail.
""" """
pass
def __init__(self, exc_info):
Exception.__init__(self)
self.exc_info = exc_info
class _UnexpectedSuccess(Exception):
"""
The test was supposed to fail, but it didn't!
"""
pass
def _id(obj):
return obj
def skip(reason):
"""
Unconditionally skip a test.
"""
def decorator(test_item):
if isinstance(test_item, type) and issubclass(test_item, TestCase):
test_item.__unittest_skip__ = True
test_item.__unittest_skip_why__ = reason
return test_item
def skip_wrapper(*args, **kwargs):
raise SkipTest(reason)
skip_wrapper.__name__ = test_item.__name__
skip_wrapper.__doc__ = test_item.__doc__
return skip_wrapper
return decorator
def skipIf(condition, reason):
"""
Skip a test if the condition is true.
"""
if condition:
return skip(reason)
return _id
def skipUnless(condition, reason):
"""
Skip a test unless the condition is true.
"""
if not condition:
return skip(reason)
return _id
def expectedFailure(func):
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except Exception:
raise _ExpectedFailure(sys.exc_info())
raise _UnexpectedSuccess
wrapper.__name__ = func.__name__
wrapper.__doc__ = func.__doc__
return wrapper
class SetupSiteError(Exception): class SetupSiteError(Exception):
""" """
...@@ -44,53 +100,6 @@ class SetupSiteError(Exception): ...@@ -44,53 +100,6 @@ class SetupSiteError(Exception):
""" """
pass pass
def _id(obj):
return obj
def skip(reason):
"""
Unconditionally skip a test.
"""
def decorator(test_item):
if isinstance(test_item, type) and issubclass(test_item, TestCase):
test_item.__unittest_skip__ = True
test_item.__unittest_skip_why__ = reason
return test_item
def skip_wrapper(*args, **kwargs):
raise SkipTest(reason)
skip_wrapper.__name__ = test_item.__name__
skip_wrapper.__doc__ = test_item.__doc__
return skip_wrapper
return decorator
def skipIf(condition, reason):
"""
Skip a test if the condition is true.
"""
if condition:
return skip(reason)
return _id
def skipUnless(condition, reason):
"""
Skip a test unless the condition is true.
"""
if not condition:
return skip(reason)
return _id
def expectedFailure(func):
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except Exception:
raise _ExpectedFailure(sys.exc_info())
raise _UnexpectedSuccess
wrapper.__name__ = func.__name__
wrapper.__doc__ = func.__doc__
return wrapper
class TestCase(unittest.TestCase): class TestCase(unittest.TestCase):
"""We redefine here the run() method, and add a skipTest() method. """We redefine here the run() method, and add a skipTest() method.
""" """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment