Commits

Georg Brandl  committed 6aa428b

Add skip_if and skip_unless test decorators.

  • Participants
  • Parent commits 321754e

Comments (0)

Files changed (1)

File tests/util.py

 
 from path import path
 
-from nose import tools
+from nose import tools, SkipTest
 
 
 __all__ = [
     'test_root',
-    'raises', 'raises_msg', 'Struct',
+    'raises', 'raises_msg', 'skip_if', 'skip_unless', 'Struct',
     'ListOutput', 'TestApp', 'with_app', 'gen_with_app',
     'path', 'with_tempdir', 'write_file',
     'sprint', 'remove_unicode_literals',
         raise AssertionError('%s did not raise %s' %
                              (func.__name__, _excstr(exc)))
 
+def skip_if(condition, msg=None):
+    """Decorator to skip test if condition is true."""
+    def deco(test):
+        @tools.make_decorator(test)
+        def skipper(*args, **kwds):
+            if condition:
+                raise SkipTest(msg or 'conditional skip')
+            return test(*args, **kwds)
+        return skipper
+    return deco
+
+def skip_unless(condition, msg=None):
+    """Decorator to skip test if condition is false."""
+    return skip_if(not condition, msg)
+
 
 class Struct(object):
     def __init__(self, **kwds):