Show More
@@ -0,0 +1,161 b'' | |||
|
1 | """Tests for the decorators we've created for IPython. | |
|
2 | """ | |
|
3 | ||
|
4 | # Module imports | |
|
5 | # Std lib | |
|
6 | import inspect | |
|
7 | import sys | |
|
8 | ||
|
9 | # Third party | |
|
10 | import nose.tools as nt | |
|
11 | ||
|
12 | # Our own | |
|
13 | from IPython.testing import decorators as dec | |
|
14 | ||
|
15 | ||
|
16 | #----------------------------------------------------------------------------- | |
|
17 | # Utilities | |
|
18 | ||
|
19 | # Note: copied from OInspect, kept here so the testing stuff doesn't create | |
|
20 | # circular dependencies and is easier to reuse. | |
|
21 | def getargspec(obj): | |
|
22 | """Get the names and default values of a function's arguments. | |
|
23 | ||
|
24 | A tuple of four things is returned: (args, varargs, varkw, defaults). | |
|
25 | 'args' is a list of the argument names (it may contain nested lists). | |
|
26 | 'varargs' and 'varkw' are the names of the * and ** arguments or None. | |
|
27 | 'defaults' is an n-tuple of the default values of the last n arguments. | |
|
28 | ||
|
29 | Modified version of inspect.getargspec from the Python Standard | |
|
30 | Library.""" | |
|
31 | ||
|
32 | if inspect.isfunction(obj): | |
|
33 | func_obj = obj | |
|
34 | elif inspect.ismethod(obj): | |
|
35 | func_obj = obj.im_func | |
|
36 | else: | |
|
37 | raise TypeError, 'arg is not a Python function' | |
|
38 | args, varargs, varkw = inspect.getargs(func_obj.func_code) | |
|
39 | return args, varargs, varkw, func_obj.func_defaults | |
|
40 | ||
|
41 | #----------------------------------------------------------------------------- | |
|
42 | # Testing functions | |
|
43 | ||
|
44 | @dec.skip | |
|
45 | def test_deliberately_broken(): | |
|
46 | """A deliberately broken test - we want to skip this one.""" | |
|
47 | 1/0 | |
|
48 | ||
|
49 | @dec.skip('foo') | |
|
50 | def test_deliberately_broken2(): | |
|
51 | """Another deliberately broken test - we want to skip this one.""" | |
|
52 | 1/0 | |
|
53 | ||
|
54 | ||
|
55 | # Verify that we can correctly skip the doctest for a function at will, but | |
|
56 | # that the docstring itself is NOT destroyed by the decorator. | |
|
57 | @dec.skip_doctest | |
|
58 | def doctest_bad(x,y=1,**k): | |
|
59 | """A function whose doctest we need to skip. | |
|
60 | ||
|
61 | >>> 1+1 | |
|
62 | 3 | |
|
63 | """ | |
|
64 | print 'x:',x | |
|
65 | print 'y:',y | |
|
66 | print 'k:',k | |
|
67 | ||
|
68 | ||
|
69 | def call_doctest_bad(): | |
|
70 | """Check that we can still call the decorated functions. | |
|
71 | ||
|
72 | >>> doctest_bad(3,y=4) | |
|
73 | x: 3 | |
|
74 | y: 4 | |
|
75 | k: {} | |
|
76 | """ | |
|
77 | pass | |
|
78 | ||
|
79 | ||
|
80 | def test_skip_dt_decorator(): | |
|
81 | """Doctest-skipping decorator should preserve the docstring. | |
|
82 | """ | |
|
83 | # Careful: 'check' must be a *verbatim* copy of the doctest_bad docstring! | |
|
84 | check = """A function whose doctest we need to skip. | |
|
85 | ||
|
86 | >>> 1+1 | |
|
87 | 3 | |
|
88 | """ | |
|
89 | # Fetch the docstring from doctest_bad after decoration. | |
|
90 | val = doctest_bad.__doc__ | |
|
91 | ||
|
92 | assert check==val,"doctest_bad docstrings don't match" | |
|
93 | ||
|
94 | # Doctest skipping should work for class methods too | |
|
95 | class foo(object): | |
|
96 | """Foo | |
|
97 | ||
|
98 | Example: | |
|
99 | ||
|
100 | >>> 1+1 | |
|
101 | 2 | |
|
102 | """ | |
|
103 | ||
|
104 | @dec.skip_doctest | |
|
105 | def __init__(self,x): | |
|
106 | """Make a foo. | |
|
107 | ||
|
108 | Example: | |
|
109 | ||
|
110 | >>> f = foo(3) | |
|
111 | junk | |
|
112 | """ | |
|
113 | print 'Making a foo.' | |
|
114 | self.x = x | |
|
115 | ||
|
116 | @dec.skip_doctest | |
|
117 | def bar(self,y): | |
|
118 | """Example: | |
|
119 | ||
|
120 | >>> f = foo(3) | |
|
121 | >>> f.bar(0) | |
|
122 | boom! | |
|
123 | >>> 1/0 | |
|
124 | bam! | |
|
125 | """ | |
|
126 | return 1/y | |
|
127 | ||
|
128 | def baz(self,y): | |
|
129 | """Example: | |
|
130 | ||
|
131 | >>> f = foo(3) | |
|
132 | Making a foo. | |
|
133 | >>> f.baz(3) | |
|
134 | True | |
|
135 | """ | |
|
136 | return self.x==y | |
|
137 | ||
|
138 | ||
|
139 | ||
|
140 | def test_skip_dt_decorator2(): | |
|
141 | """Doctest-skipping decorator should preserve function signature. | |
|
142 | """ | |
|
143 | # Hardcoded correct answer | |
|
144 | dtargs = (['x', 'y'], None, 'k', (1,)) | |
|
145 | # Introspect out the value | |
|
146 | dtargsr = getargspec(doctest_bad) | |
|
147 | assert dtargsr==dtargs, \ | |
|
148 | "Incorrectly reconstructed args for doctest_bad: %s" % (dtargsr,) | |
|
149 | ||
|
150 | ||
|
151 | @dec.skip_linux | |
|
152 | def test_linux(): | |
|
153 | nt.assert_not_equals(sys.platform,'linux2',"This test can't run under linux") | |
|
154 | ||
|
155 | @dec.skip_win32 | |
|
156 | def test_win32(): | |
|
157 | nt.assert_not_equals(sys.platform,'win32',"This test can't run under windows") | |
|
158 | ||
|
159 | @dec.skip_osx | |
|
160 | def test_osx(): | |
|
161 | nt.assert_not_equals(sys.platform,'darwin',"This test can't run under osx") |
@@ -13,15 +13,17 b' __docformat__ = "restructuredtext en"' | |||
|
13 | 13 | #------------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | |
|
16 | # Stdlib imports | |
|
16 | 17 | import os |
|
17 | 18 | from cStringIO import StringIO |
|
18 | 19 | |
|
19 | # FIXME: | |
|
20 | import nose | |
|
21 | import sys | |
|
22 | if sys.platform == 'win32': | |
|
23 | raise nose.SkipTest("These tests are not reliable under windows") | |
|
20 | # Our own imports | |
|
21 | from IPython.testing import decorators as dec | |
|
24 | 22 | |
|
23 | #----------------------------------------------------------------------------- | |
|
24 | # Test functions | |
|
25 | ||
|
26 | @dec.skip_win32 | |
|
25 | 27 | def test_redirector(): |
|
26 | 28 | """ Checks that the redirector can be used to do synchronous capture. |
|
27 | 29 | """ |
@@ -42,6 +44,8 b' def test_redirector():' | |||
|
42 | 44 | result2 = "".join("%ic\n%i\n" %(i, i) for i in range(10)) |
|
43 | 45 | assert result1 == result2 |
|
44 | 46 | |
|
47 | ||
|
48 | @dec.skip_win32 | |
|
45 | 49 | def test_redirector_output_trap(): |
|
46 | 50 | """ This test check not only that the redirector_output_trap does |
|
47 | 51 | trap the output, but also that it does it in a gready way, that |
@@ -63,6 +67,4 b' def test_redirector_output_trap():' | |||
|
63 | 67 | result1 = out.getvalue() |
|
64 | 68 | result2 = "".join("%ic\n%ip\n%i\n" %(i, i, i) for i in range(10)) |
|
65 | 69 | assert result1 == result2 |
|
66 | ||
|
67 | 70 | |
|
68 |
@@ -8,6 +8,10 b' 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 | This module provides a set of useful decorators meant to be ready to use in | |
|
12 | your own tests. See the bottom of the file for the ready-made ones, and if you | |
|
13 | find yourself writing a new one that may be of generic use, add it here. | |
|
14 | ||
|
11 | 15 | NOTE: This file contains IPython-specific decorators and imports the |
|
12 | 16 | numpy.testing.decorators file, which we've copied verbatim. Any of our own |
|
13 | 17 | code will be added at the bottom if we end up extending this. |
@@ -15,6 +19,7 b' code will be added at the bottom if we end up extending this.' | |||
|
15 | 19 | |
|
16 | 20 | # Stdlib imports |
|
17 | 21 | import inspect |
|
22 | import sys | |
|
18 | 23 | |
|
19 | 24 | # Third-party imports |
|
20 | 25 | |
@@ -123,6 +128,9 b" skip_doctest = make_label_dec('skip_doctest'," | |||
|
123 | 128 | def skip(msg=''): |
|
124 | 129 | """Decorator - mark a test function for skipping from test suite. |
|
125 | 130 | |
|
131 | This function *is* already a decorator, it is not a factory like | |
|
132 | make_label_dec or some of those in decorators_numpy. | |
|
133 | ||
|
126 | 134 | :Parameters: |
|
127 | 135 | |
|
128 | 136 | func : function |
@@ -145,3 +153,8 b" def skip(msg=''):" | |||
|
145 | 153 | return apply_wrapper(wrapper,func) |
|
146 | 154 | |
|
147 | 155 | return inner |
|
156 | ||
|
157 | # Decorators to skip certain tests on specific platforms. | |
|
158 | skip_win32 = skipif(sys.platform=='win32',"This test does not run under Windows") | |
|
159 | skip_linux = skipif(sys.platform=='linux2',"This test does not run under Linux") | |
|
160 | skip_osx = skipif(sys.platform=='darwin',"This test does not run under OSX") |
@@ -1,152 +1,19 b'' | |||
|
1 | """Some simple tests for the plugin while running scripts. | |
|
2 | """ | |
|
1 | 3 | # Module imports |
|
2 | 4 | # Std lib |
|
3 | 5 | import inspect |
|
4 | 6 | |
|
5 | # Third party | |
|
6 | ||
|
7 | 7 | # Our own |
|
8 | 8 | from IPython.testing import decorators as dec |
|
9 | 9 | |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | # Utilities | |
|
12 | ||
|
13 | # Note: copied from OInspect, kept here so the testing stuff doesn't create | |
|
14 | # circular dependencies and is easier to reuse. | |
|
15 | def getargspec(obj): | |
|
16 | """Get the names and default values of a function's arguments. | |
|
17 | ||
|
18 | A tuple of four things is returned: (args, varargs, varkw, defaults). | |
|
19 | 'args' is a list of the argument names (it may contain nested lists). | |
|
20 | 'varargs' and 'varkw' are the names of the * and ** arguments or None. | |
|
21 | 'defaults' is an n-tuple of the default values of the last n arguments. | |
|
22 | ||
|
23 | Modified version of inspect.getargspec from the Python Standard | |
|
24 | Library.""" | |
|
25 | ||
|
26 | if inspect.isfunction(obj): | |
|
27 | func_obj = obj | |
|
28 | elif inspect.ismethod(obj): | |
|
29 | func_obj = obj.im_func | |
|
30 | else: | |
|
31 | raise TypeError, 'arg is not a Python function' | |
|
32 | args, varargs, varkw = inspect.getargs(func_obj.func_code) | |
|
33 | return args, varargs, varkw, func_obj.func_defaults | |
|
34 | ||
|
35 | #----------------------------------------------------------------------------- | |
|
36 | 11 | # Testing functions |
|
37 | 12 | |
|
38 | 13 | def test_trivial(): |
|
39 | 14 | """A trivial passing test.""" |
|
40 | 15 | pass |
|
41 | 16 | |
|
42 | ||
|
43 | @dec.skip | |
|
44 | def test_deliberately_broken(): | |
|
45 | """A deliberately broken test - we want to skip this one.""" | |
|
46 | 1/0 | |
|
47 | ||
|
48 | @dec.skip('foo') | |
|
49 | def test_deliberately_broken2(): | |
|
50 | """Another deliberately broken test - we want to skip this one.""" | |
|
51 | 1/0 | |
|
52 | ||
|
53 | ||
|
54 | # Verify that we can correctly skip the doctest for a function at will, but | |
|
55 | # that the docstring itself is NOT destroyed by the decorator. | |
|
56 | @dec.skip_doctest | |
|
57 | def doctest_bad(x,y=1,**k): | |
|
58 | """A function whose doctest we need to skip. | |
|
59 | ||
|
60 | >>> 1+1 | |
|
61 | 3 | |
|
62 | """ | |
|
63 | print 'x:',x | |
|
64 | print 'y:',y | |
|
65 | print 'k:',k | |
|
66 | ||
|
67 | ||
|
68 | def call_doctest_bad(): | |
|
69 | """Check that we can still call the decorated functions. | |
|
70 | ||
|
71 | >>> doctest_bad(3,y=4) | |
|
72 | x: 3 | |
|
73 | y: 4 | |
|
74 | k: {} | |
|
75 | """ | |
|
76 | pass | |
|
77 | ||
|
78 | ||
|
79 | # Doctest skipping should work for class methods too | |
|
80 | class foo(object): | |
|
81 | """Foo | |
|
82 | ||
|
83 | Example: | |
|
84 | ||
|
85 | >>> 1+1 | |
|
86 | 2 | |
|
87 | """ | |
|
88 | ||
|
89 | @dec.skip_doctest | |
|
90 | def __init__(self,x): | |
|
91 | """Make a foo. | |
|
92 | ||
|
93 | Example: | |
|
94 | ||
|
95 | >>> f = foo(3) | |
|
96 | junk | |
|
97 | """ | |
|
98 | print 'Making a foo.' | |
|
99 | self.x = x | |
|
100 | ||
|
101 | @dec.skip_doctest | |
|
102 | def bar(self,y): | |
|
103 | """Example: | |
|
104 | ||
|
105 | >>> f = foo(3) | |
|
106 | >>> f.bar(0) | |
|
107 | boom! | |
|
108 | >>> 1/0 | |
|
109 | bam! | |
|
110 | """ | |
|
111 | return 1/y | |
|
112 | ||
|
113 | def baz(self,y): | |
|
114 | """Example: | |
|
115 | ||
|
116 | >>> f = foo(3) | |
|
117 | Making a foo. | |
|
118 | >>> f.baz(3) | |
|
119 | True | |
|
120 | """ | |
|
121 | return self.x==y | |
|
122 | ||
|
123 | ||
|
124 | def test_skip_dt_decorator(): | |
|
125 | """Doctest-skipping decorator should preserve the docstring. | |
|
126 | """ | |
|
127 | # Careful: 'check' must be a *verbatim* copy of the doctest_bad docstring! | |
|
128 | check = """A function whose doctest we need to skip. | |
|
129 | ||
|
130 | >>> 1+1 | |
|
131 | 3 | |
|
132 | """ | |
|
133 | # Fetch the docstring from doctest_bad after decoration. | |
|
134 | val = doctest_bad.__doc__ | |
|
135 | ||
|
136 | assert check==val,"doctest_bad docstrings don't match" | |
|
137 | ||
|
138 | ||
|
139 | def test_skip_dt_decorator2(): | |
|
140 | """Doctest-skipping decorator should preserve function signature. | |
|
141 | """ | |
|
142 | # Hardcoded correct answer | |
|
143 | dtargs = (['x', 'y'], None, 'k', (1,)) | |
|
144 | # Introspect out the value | |
|
145 | dtargsr = getargspec(doctest_bad) | |
|
146 | assert dtargsr==dtargs, \ | |
|
147 | "Incorrectly reconstructed args for doctest_bad: %s" % (dtargsr,) | |
|
148 | ||
|
149 | ||
|
150 | 17 | def doctest_run(): |
|
151 | 18 | """Test running a trivial script. |
|
152 | 19 | |
@@ -154,7 +21,6 b' def doctest_run():' | |||
|
154 | 21 | x is: 1 |
|
155 | 22 | """ |
|
156 | 23 | |
|
157 | #@dec.skip_doctest | |
|
158 | 24 | def doctest_runvars(): |
|
159 | 25 | """Test that variables defined in scripts get loaded correcly via %run. |
|
160 | 26 |
General Comments 0
You need to be logged in to leave comments.
Login now