From 4420b320e7c447feddc870396874a46fba5dc69b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Sun, 28 Feb 2016 03:43:04 +0100 Subject: [PATCH] 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). --- tests/__init__.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/__init__.py b/tests/__init__.py index b8b6fdf8..f19ca22c 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -23,6 +23,7 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # from distutils import spawn +import functools import multiprocessing import logging @@ -31,6 +32,7 @@ import shutil import subprocess import tempfile import unittest +from unittest.case import _ExpectedFailure, _UnexpectedSuccess import lxml.etree import sys @@ -88,6 +90,32 @@ def skipUnlessGit(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): """A context manager used to implement TestCase.assertNotRaises methods.