Improve expectedFailureIfTemplate decorator

This commit is contained in:
Marek Marczykowski-Górecki 2016-03-02 14:10:50 +01:00
parent 8497471a72
commit 7ca89688bd
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724

View File

@ -32,7 +32,7 @@ import shutil
import subprocess import subprocess
import tempfile import tempfile
import unittest import unittest
from unittest.case import _ExpectedFailure, _UnexpectedSuccess import unittest.case
import lxml.etree import lxml.etree
import sys import sys
@ -99,21 +99,21 @@ def expectedFailureIfTemplate(templates):
""" """
def decorator(func): def decorator(func):
@functools.wraps(func) @functools.wraps(func)
def wrapper(*args, **kwargs): def wrapper(self, *args, **kwargs):
template = args[0].template template = self.template
if isinstance(templates, basestring): if isinstance(templates, basestring):
should_expect_fail = template in templates should_expect_fail = template in templates
else: else:
should_expect_fail = any([template in x for x in templates]) should_expect_fail = any([template in x for x in templates])
if should_expect_fail: if should_expect_fail:
try: try:
func(*args, **kwargs) func(self, *args, **kwargs)
except Exception: except Exception:
raise _ExpectedFailure(sys.exc_info()) raise unittest.case._ExpectedFailure(sys.exc_info())
raise _UnexpectedSuccess raise unittest.case._UnexpectedSuccess()
else: else:
# Call directly: # Call directly:
func(*args, **kwargs) func(self, *args, **kwargs)
return wrapper return wrapper
return decorator return decorator