1
"""Decorators for labeling test objects
2
Decorators for labeling and modifying behavior of test objects.
3
4
Decorators that merely return a modified version of the original
4
function object are straightforward. Decorators that return a new
5
function object are straightforward. Decorators that return a new
5
6
function object need to use
6
nose.tools.make_decorator(original_function)(decorator) in returning
7
the decorator, in order to preserve metadata such as function name,
8
setup and teardown functions and so on - see nose.tools for more
9
nose.tools.make_decorator(original_function)(decorator)
11
in returning the decorator, in order to preserve meta-data such as
12
function name, setup and teardown functions and so on - see
13
``nose.tools`` for more information.
19
from numpy.testing.utils import \
20
WarningManager, WarningMessage
14
"""Labels a test as 'slow'.
24
Label a test as 'slow'.
16
26
The exact definition of a slow test is obviously both subjective and
17
27
hardware-dependent, but in general any individual test that requires more
18
28
than a second or two should be labeled as slow (the whole suite consits of
19
thousands of tests, so even a second is significant)."""
29
thousands of tests, so even a second is significant).
34
The test to label as slow.
39
The decorated test `t`.
43
The `numpy.testing` module includes ``import decorators as dec``.
44
A test can be decorated as slow like this::
46
from numpy.testing import *
50
print 'Big, slow test'
24
57
def setastest(tf=True):
25
''' Signals to nose that this function is or is not a test
59
Signals to nose that this function is or is not a test.
30
If True specifies this is a test, not a test otherwise
33
>>> from numpy.testing.decorators import setastest
35
... def func_with_test_in_name(arg1, arg2): pass
39
This decorator cannot use the nose namespace, because it can be
40
called from a non-test module. See also istest and nottest in
64
If True, specifies that the decorated callable is a test.
65
If False, specifies that the decorated callable is not a test.
70
This decorator can't use the nose namespace, because it can be
71
called from a non-test module. See also ``istest`` and ``nottest`` in
76
`setastest` can be used in the following way::
78
from numpy.testing.decorators import setastest
81
def func_with_test_in_name(arg1, arg2):
49
90
def skipif(skip_condition, msg=None):
50
''' Make function raise SkipTest exception if skip_condition is true
92
Make function raise SkipTest exception if a given condition is true.
94
If the condition is a callable, it is used at runtime to dynamically
95
make the decision. This is useful for tests that may require costly
96
imports, to delay the cost until the test suite is actually executed.
54
skip_condition : bool or callable.
55
Flag to determine whether to skip test. If the condition is a
56
callable, it is used at runtime to dynamically make the decision. This
57
is useful for tests that may require costly imports, to delay the cost
58
until the test suite is actually executed.
60
Message to give on raising a SkipTest exception
100
skip_condition : bool or callable
101
Flag to determine whether to skip the decorated test.
103
Message to give on raising a SkipTest exception. Default is None.
65
Decorator, which, when applied to a function, causes SkipTest
66
to be raised when the skip_condition was True, and the function
67
to be called normally otherwise.
108
Decorator which, when applied to a function, causes SkipTest
109
to be raised when `skip_condition` is True, and the function
110
to be called normally otherwise.
71
You will see from the code that we had to further decorate the
72
decorator with the nose.tools.make_decorator function in order to
73
transmit function name, and various other metadata.
114
The decorator itself is decorated with the ``nose.tools.make_decorator``
115
function in order to transmit function name, and various other metadata.
76
119
def skip_decorator(f):
77
120
# Local import to avoid a hard nose dependency and only incur the
124
167
def knownfailureif(fail_condition, msg=None):
125
''' Make function raise KnownFailureTest exception if fail_condition is true
169
Make function raise KnownFailureTest exception if given condition is true.
171
If the condition is a callable, it is used at runtime to dynamically
172
make the decision. This is useful for tests that may require costly
173
imports, to delay the cost until the test suite is actually executed.
129
fail_condition : bool or callable.
130
Flag to determine whether to mark test as known failure (True)
131
or not (False). If the condition is a callable, it is used at
132
runtime to dynamically make the decision. This is useful for
133
tests that may require costly imports, to delay the cost
134
until the test suite is actually executed.
136
Message to give on raising a KnownFailureTest exception
177
fail_condition : bool or callable
178
Flag to determine whether to mark the decorated test as a known
179
failure (if True) or not (if False).
181
Message to give on raising a KnownFailureTest exception.
141
Decorator, which, when applied to a function, causes SkipTest
142
to be raised when the skip_condition was True, and the function
143
to be called normally otherwise.
187
Decorator, which, when applied to a function, causes SkipTest
188
to be raised when `skip_condition` is True, and the function
189
to be called normally otherwise.
147
You will see from the code that we had to further decorate the
148
decorator with the nose.tools.make_decorator function in order to
149
transmit function name, and various other metadata.
193
The decorator itself is decorated with the ``nose.tools.make_decorator``
194
function in order to transmit function name, and various other metadata.
152
198
msg = 'Test skipped due to known failure'
170
216
return nose.tools.make_decorator(f)(knownfailer)
172
218
return knownfail_decorator
220
def deprecated(conditional=True):
222
Filter deprecation warnings while running the test suite.
224
This decorator can be used to filter DeprecationWarning's, to avoid
225
printing them during the test suite run, while checking that the test
226
actually raises a DeprecationWarning.
230
conditional : bool or callable, optional
231
Flag to determine whether to mark test as deprecated or not. If the
232
condition is a callable, it is used at runtime to dynamically make the
233
decision. Default is True.
238
The `deprecated` decorator itself.
242
.. versionadded:: 1.4.0
245
def deprecate_decorator(f):
246
# Local import to avoid a hard nose dependency and only incur the
247
# import time overhead at actual test-time.
249
from noseclasses import KnownFailureTest
251
def _deprecated_imp(*args, **kwargs):
252
# Poor man's replacement for the with statement
253
ctx = WarningManager(record=True)
255
warnings.simplefilter('always')
259
raise AssertionError("No warning raised when calling %s"
261
if not l[0].category is DeprecationWarning:
262
raise AssertionError("First warning for %s is not a " \
263
"DeprecationWarning( is %s)" % (f.__name__, l[0]))
267
if callable(conditional):
272
return nose.tools.make_decorator(f)(_deprecated_imp)
275
return deprecate_decorator