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