Skip to content

Commit

Permalink
Add new decorator to raise xfail on known exceptions
Browse files Browse the repository at this point in the history
This commit adds a new decorator to TestCase that will take in a
exception class and when it's raised by a test it will be converted
to _ExpectedFailure. If a different exception is raised by the test
it will just passthrough and be handled like normal.
  • Loading branch information
mtreinish committed Mar 7, 2016
1 parent 4d4bc6f commit 1847c77
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions testtools/testcase.py
Expand Up @@ -108,6 +108,27 @@ def wrapper(*args, **kwargs):
return wrapper


def xfail_on_exc(*args, **kwargs):
"""A test decorator to catch a specific exception and handle it as xfail.
If an exception is raised other than the expected exception type it will
passthrough and be handled like normal.
:param Exception exc: The exception class to catch and treat as
"""
def decorator(f):
if len(args) == 0:
raise TypeError('A expected exception needs to be provided')

This comment has been minimized.

Copy link
@mriedem

mriedem Mar 7, 2016

I might word this as "An expected exception, or list of exceptions, needs to be provided." since you could expect multiple expected exceptions, right?

expected_exception = args[0]
@functools.wraps(func)
def wrapper(self, *func_args, **func_kwargs):
try:
func(*func_args, **func_kwargs)
except expected_exception:
raise _ExpectedFailure(sys.exc_info())
return wrapper


def run_test_with(test_runner, **kwargs):
"""Decorate a test as using a specific ``RunTest``.
Expand Down

0 comments on commit 1847c77

Please sign in to comment.