Make function raise SkipTest exception if a given condition is true. If the condition is a callable, it is used at runtime to dynamically make the decision. This is useful for tests that may require costly imports, to delay the cost until the test suite is actually executed. P
(skip_condition, msg=None)
| 25 | |
| 26 | |
| 27 | def skipif(skip_condition, msg=None): |
| 28 | """ |
| 29 | Make function raise SkipTest exception if a given condition is true. |
| 30 | |
| 31 | If the condition is a callable, it is used at runtime to dynamically |
| 32 | make the decision. This is useful for tests that may require costly |
| 33 | imports, to delay the cost until the test suite is actually executed. |
| 34 | |
| 35 | Parameters |
| 36 | ---------- |
| 37 | skip_condition : bool or callable |
| 38 | Flag to determine whether to skip the decorated test. |
| 39 | msg : str, optional |
| 40 | Message to give on raising a SkipTest exception. Default is None. |
| 41 | |
| 42 | Returns |
| 43 | ------- |
| 44 | decorator : function |
| 45 | Decorator which, when applied to a function, causes SkipTest |
| 46 | to be raised when `skip_condition` is True, and the function |
| 47 | to be called normally otherwise. |
| 48 | |
| 49 | Notes |
| 50 | ----- |
| 51 | The decorator itself is decorated with the ``nose.tools.make_decorator`` |
| 52 | function in order to transmit function name, and various other metadata. |
| 53 | |
| 54 | """ |
| 55 | |
| 56 | def skip_decorator(f): |
| 57 | # Local import to avoid a hard nose dependency and only incur the |
| 58 | # import time overhead at actual test-time. |
| 59 | import nose |
| 60 | |
| 61 | # Allow for both boolean or callable skip conditions. |
| 62 | if callable(skip_condition): |
| 63 | skip_val = lambda : skip_condition() |
| 64 | else: |
| 65 | skip_val = lambda : skip_condition |
| 66 | |
| 67 | def get_msg(func,msg=None): |
| 68 | """Skip message with information about function being skipped.""" |
| 69 | if msg is None: |
| 70 | out = 'Test skipped due to test condition' |
| 71 | else: |
| 72 | out = '\n'+msg |
| 73 | |
| 74 | return "Skipping test: %s%s" % (func.__name__,out) |
| 75 | |
| 76 | # We need to define *two* skippers because Python doesn't allow both |
| 77 | # return with value and yield inside the same function. |
| 78 | def skipper_func(*args, **kwargs): |
| 79 | """Skipper for normal test functions.""" |
| 80 | if skip_val(): |
| 81 | raise nose.SkipTest(get_msg(f,msg)) |
| 82 | else: |
| 83 | return f(*args, **kwargs) |
| 84 |
nothing calls this directly
no outgoing calls
no test coverage detected