tests: add expectedFailureIfTemplate decorator

Some tests are expected to fail only on some templates (some feature not
available in older distribution, some feature not yet ported to
another).
Cette révision appartient à :
Marek Marczykowski-Górecki 2016-02-28 03:43:04 +01:00
Parent 280a0743c2
révision 4420b320e7
Signature inconnue de Gitea
ID de la clé GPG: 063938BA42CFA724

Voir le fichier

@ -23,6 +23,7 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# #
from distutils import spawn from distutils import spawn
import functools
import multiprocessing import multiprocessing
import logging import logging
@ -31,6 +32,7 @@ import shutil
import subprocess import subprocess
import tempfile import tempfile
import unittest import unittest
from unittest.case import _ExpectedFailure, _UnexpectedSuccess
import lxml.etree import lxml.etree
import sys import sys
@ -88,6 +90,32 @@ def skipUnlessGit(test_item):
return unittest.skipUnless(in_git, 'outside git tree')(test_item) return unittest.skipUnless(in_git, 'outside git tree')(test_item)
def expectedFailureIfTemplate(templates):
"""
Decorator for marking specific test as expected to fail only for some
templates. Template name is compared as substring, so 'whonix' will
handle both 'whonix-ws' and 'whonix-gw'.
templates can be either a single string, or an iterable
"""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
template = args[0].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)
except Exception:
raise _ExpectedFailure(sys.exc_info())
raise _UnexpectedSuccess
else:
# Call directly:
func(*args, **kwargs)
return wrapper
return decorator
class _AssertNotRaisesContext(object): class _AssertNotRaisesContext(object):
"""A context manager used to implement TestCase.assertNotRaises methods. """A context manager used to implement TestCase.assertNotRaises methods.