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