Show More
@@ -1,94 +1,97 | |||
|
1 | 1 | """Decorators for labeling test objects |
|
2 | 2 | |
|
3 | 3 | Decorators that merely return a modified version of the original |
|
4 | 4 | function object are straightforward. Decorators that return a new |
|
5 | 5 | function object need to use |
|
6 | 6 | nose.tools.make_decorator(original_function)(decorator) in returning |
|
7 | 7 | the decorator, in order to preserve metadata such as function name, |
|
8 | 8 | setup and teardown functions and so on - see nose.tools for more |
|
9 | 9 | information. |
|
10 | 10 | |
|
11 | 11 | """ |
|
12 | 12 | |
|
13 | 13 | def slow(t): |
|
14 | 14 | """Labels a test as 'slow'. |
|
15 | 15 | |
|
16 | 16 | The exact definition of a slow test is obviously both subjective and |
|
17 | 17 | hardware-dependent, but in general any individual test that requires more |
|
18 | 18 | than a second or two should be labeled as slow (the whole suite consits of |
|
19 | 19 | thousands of tests, so even a second is significant).""" |
|
20 | 20 | |
|
21 | 21 | t.slow = True |
|
22 | 22 | return t |
|
23 | 23 | |
|
24 | 24 | def setastest(tf=True): |
|
25 | 25 | ''' Signals to nose that this function is or is not a test |
|
26 | 26 | |
|
27 | 27 | Parameters |
|
28 | 28 | ---------- |
|
29 | 29 | tf : bool |
|
30 | 30 | If True specifies this is a test, not a test otherwise |
|
31 | 31 | |
|
32 | 32 | e.g |
|
33 | 33 | >>> from numpy.testing.decorators import setastest |
|
34 | 34 | >>> @setastest(False) |
|
35 | 35 | ... def func_with_test_in_name(arg1, arg2): pass |
|
36 | 36 | ... |
|
37 | 37 | >>> |
|
38 | 38 | |
|
39 | 39 | This decorator cannot use the nose namespace, because it can be |
|
40 | 40 | called from a non-test module. See also istest and nottest in |
|
41 | 41 | nose.tools |
|
42 | 42 | |
|
43 | 43 | ''' |
|
44 | 44 | def set_test(t): |
|
45 | 45 | t.__test__ = tf |
|
46 | 46 | return t |
|
47 | 47 | return set_test |
|
48 | 48 | |
|
49 | def skipif(skip_condition, msg=None): | |
|
49 | def skipif(skip_condition=True, msg=None): | |
|
50 | 50 | ''' Make function raise SkipTest exception if skip_condition is true |
|
51 | 51 | |
|
52 | 52 | Parameters |
|
53 | 53 | --------- |
|
54 | skip_condition : bool | |
|
55 |
|
|
|
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. | |
|
56 | 59 | msg : string |
|
57 | 60 | Message to give on raising a SkipTest exception |
|
58 | 61 | |
|
59 | 62 | Returns |
|
60 | 63 | ------- |
|
61 | 64 | decorator : function |
|
62 | 65 | Decorator, which, when applied to a function, causes SkipTest |
|
63 | 66 | to be raised when the skip_condition was True, and the function |
|
64 | 67 | to be called normally otherwise. |
|
65 | 68 | |
|
66 | 69 | Notes |
|
67 | 70 | ----- |
|
68 | 71 | You will see from the code that we had to further decorate the |
|
69 | 72 | decorator with the nose.tools.make_decorator function in order to |
|
70 | 73 | transmit function name, and various other metadata. |
|
71 | 74 | ''' |
|
72 | 75 | if msg is None: |
|
73 | 76 | msg = 'Test skipped due to test condition' |
|
74 | 77 | def skip_decorator(f): |
|
75 | 78 | # Local import to avoid a hard nose dependency and only incur the |
|
76 | 79 | # import time overhead at actual test-time. |
|
77 | 80 | import nose |
|
78 | 81 | def skipper(*args, **kwargs): |
|
79 | 82 | if skip_condition: |
|
80 | 83 | raise nose.SkipTest, msg |
|
81 | 84 | else: |
|
82 | 85 | return f(*args, **kwargs) |
|
83 | 86 | return nose.tools.make_decorator(f)(skipper) |
|
84 | 87 | return skip_decorator |
|
85 | 88 | |
|
86 | 89 | def skipknownfailure(f): |
|
87 | 90 | ''' Decorator to raise SkipTest for test known to fail |
|
88 | 91 | ''' |
|
89 | 92 | # Local import to avoid a hard nose dependency and only incur the |
|
90 | 93 | # import time overhead at actual test-time. |
|
91 | 94 | import nose |
|
92 | 95 | def skipper(*args, **kwargs): |
|
93 | 96 | raise nose.SkipTest, 'This test is known to fail' |
|
94 | 97 | return nose.tools.make_decorator(f)(skipper) |
General Comments 0
You need to be logged in to leave comments.
Login now