Make function raise SkipTest exception if skip_condition is true Parameters ---------- skip_condition : bool or callable Flag to determine whether to skip test. If the condition is a callable, it is used at runtime to dynamically make the decision. This is useful for
(skip_condition, msg=None)
| 158 | # preserve function metadata better and allows the skip condition to be a |
| 159 | # callable. |
| 160 | def skipif(skip_condition, msg=None): |
| 161 | ''' Make function raise SkipTest exception if skip_condition is true |
| 162 | |
| 163 | Parameters |
| 164 | ---------- |
| 165 | |
| 166 | skip_condition : bool or callable |
| 167 | Flag to determine whether to skip test. If the condition is a |
| 168 | callable, it is used at runtime to dynamically make the decision. This |
| 169 | is useful for tests that may require costly imports, to delay the cost |
| 170 | until the test suite is actually executed. |
| 171 | msg : string |
| 172 | Message to give on raising a SkipTest exception. |
| 173 | |
| 174 | Returns |
| 175 | ------- |
| 176 | decorator : function |
| 177 | Decorator, which, when applied to a function, causes SkipTest |
| 178 | to be raised when the skip_condition was True, and the function |
| 179 | to be called normally otherwise. |
| 180 | |
| 181 | Notes |
| 182 | ----- |
| 183 | You will see from the code that we had to further decorate the |
| 184 | decorator with the nose.tools.make_decorator function in order to |
| 185 | transmit function name, and various other metadata. |
| 186 | ''' |
| 187 | |
| 188 | def skip_decorator(f): |
| 189 | # Local import to avoid a hard nose dependency and only incur the |
| 190 | # import time overhead at actual test-time. |
| 191 | import nose |
| 192 | |
| 193 | # Allow for both boolean or callable skip conditions. |
| 194 | if callable(skip_condition): |
| 195 | skip_val = skip_condition |
| 196 | else: |
| 197 | skip_val = lambda : skip_condition |
| 198 | |
| 199 | def get_msg(func,msg=None): |
| 200 | """Skip message with information about function being skipped.""" |
| 201 | if msg is None: out = 'Test skipped due to test condition.' |
| 202 | else: out = msg |
| 203 | return "Skipping test: %s. %s" % (func.__name__,out) |
| 204 | |
| 205 | # We need to define *two* skippers because Python doesn't allow both |
| 206 | # return with value and yield inside the same function. |
| 207 | def skipper_func(*args, **kwargs): |
| 208 | """Skipper for normal test functions.""" |
| 209 | if skip_val(): |
| 210 | raise nose.SkipTest(get_msg(f,msg)) |
| 211 | else: |
| 212 | return f(*args, **kwargs) |
| 213 | |
| 214 | def skipper_gen(*args, **kwargs): |
| 215 | """Skipper for test generators.""" |
| 216 | if skip_val(): |
| 217 | raise nose.SkipTest(get_msg(f,msg)) |
no outgoing calls
no test coverage detected