Show More
@@ -1,187 +1,168 b'' | |||||
1 | """Tests for the decorators we've created for IPython. |
|
1 | """Tests for the decorators we've created for IPython. | |
2 | """ |
|
2 | """ | |
3 |
|
3 | |||
4 | # Module imports |
|
4 | # Module imports | |
5 | # Std lib |
|
5 | # Std lib | |
6 | import inspect |
|
6 | import inspect | |
7 | import sys |
|
7 | import sys | |
8 |
|
8 | |||
9 | # Third party |
|
9 | # Third party | |
10 | import nose.tools as nt |
|
10 | import nose.tools as nt | |
11 |
|
11 | |||
12 | # Our own |
|
12 | # Our own | |
13 | from IPython.testing import decorators as dec |
|
13 | from IPython.testing import decorators as dec | |
14 | from IPython.testing.skipdoctest import skip_doctest |
|
14 | from IPython.testing.skipdoctest import skip_doctest | |
15 | from IPython.testing.ipunittest import ParametricTestCase |
|
|||
16 |
|
15 | |||
17 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
18 | # Utilities |
|
17 | # Utilities | |
19 |
|
18 | |||
20 | # Note: copied from OInspect, kept here so the testing stuff doesn't create |
|
19 | # Note: copied from OInspect, kept here so the testing stuff doesn't create | |
21 | # circular dependencies and is easier to reuse. |
|
20 | # circular dependencies and is easier to reuse. | |
22 | def getargspec(obj): |
|
21 | def getargspec(obj): | |
23 | """Get the names and default values of a function's arguments. |
|
22 | """Get the names and default values of a function's arguments. | |
24 |
|
23 | |||
25 | A tuple of four things is returned: (args, varargs, varkw, defaults). |
|
24 | A tuple of four things is returned: (args, varargs, varkw, defaults). | |
26 | 'args' is a list of the argument names (it may contain nested lists). |
|
25 | 'args' is a list of the argument names (it may contain nested lists). | |
27 | 'varargs' and 'varkw' are the names of the * and ** arguments or None. |
|
26 | 'varargs' and 'varkw' are the names of the * and ** arguments or None. | |
28 | 'defaults' is an n-tuple of the default values of the last n arguments. |
|
27 | 'defaults' is an n-tuple of the default values of the last n arguments. | |
29 |
|
28 | |||
30 | Modified version of inspect.getargspec from the Python Standard |
|
29 | Modified version of inspect.getargspec from the Python Standard | |
31 | Library.""" |
|
30 | Library.""" | |
32 |
|
31 | |||
33 | if inspect.isfunction(obj): |
|
32 | if inspect.isfunction(obj): | |
34 | func_obj = obj |
|
33 | func_obj = obj | |
35 | elif inspect.ismethod(obj): |
|
34 | elif inspect.ismethod(obj): | |
36 | func_obj = obj.im_func |
|
35 | func_obj = obj.im_func | |
37 | else: |
|
36 | else: | |
38 | raise TypeError('arg is not a Python function') |
|
37 | raise TypeError('arg is not a Python function') | |
39 | args, varargs, varkw = inspect.getargs(func_obj.func_code) |
|
38 | args, varargs, varkw = inspect.getargs(func_obj.func_code) | |
40 | return args, varargs, varkw, func_obj.func_defaults |
|
39 | return args, varargs, varkw, func_obj.func_defaults | |
41 |
|
40 | |||
42 | #----------------------------------------------------------------------------- |
|
41 | #----------------------------------------------------------------------------- | |
43 | # Testing functions |
|
42 | # Testing functions | |
44 |
|
43 | |||
45 | @dec.as_unittest |
|
44 | @dec.as_unittest | |
46 | def trivial(): |
|
45 | def trivial(): | |
47 | """A trivial test""" |
|
46 | """A trivial test""" | |
48 | pass |
|
47 | pass | |
49 |
|
48 | |||
50 | # Some examples of parametric tests. |
|
|||
51 |
|
||||
52 | def is_smaller(i,j): |
|
|||
53 | assert i<j,"%s !< %s" % (i,j) |
|
|||
54 |
|
||||
55 | class Tester(ParametricTestCase): |
|
|||
56 |
|
||||
57 | def test_parametric(self): |
|
|||
58 | yield is_smaller(3, 4) |
|
|||
59 | x, y = 1, 2 |
|
|||
60 | yield is_smaller(x, y) |
|
|||
61 |
|
||||
62 | @dec.parametric |
|
|||
63 | def test_par_standalone(): |
|
|||
64 | yield is_smaller(3, 4) |
|
|||
65 | x, y = 1, 2 |
|
|||
66 | yield is_smaller(x, y) |
|
|||
67 |
|
||||
68 |
|
49 | |||
69 | @dec.skip |
|
50 | @dec.skip | |
70 | def test_deliberately_broken(): |
|
51 | def test_deliberately_broken(): | |
71 | """A deliberately broken test - we want to skip this one.""" |
|
52 | """A deliberately broken test - we want to skip this one.""" | |
72 | 1/0 |
|
53 | 1/0 | |
73 |
|
54 | |||
74 | @dec.skip('Testing the skip decorator') |
|
55 | @dec.skip('Testing the skip decorator') | |
75 | def test_deliberately_broken2(): |
|
56 | def test_deliberately_broken2(): | |
76 | """Another deliberately broken test - we want to skip this one.""" |
|
57 | """Another deliberately broken test - we want to skip this one.""" | |
77 | 1/0 |
|
58 | 1/0 | |
78 |
|
59 | |||
79 |
|
60 | |||
80 | # Verify that we can correctly skip the doctest for a function at will, but |
|
61 | # Verify that we can correctly skip the doctest for a function at will, but | |
81 | # that the docstring itself is NOT destroyed by the decorator. |
|
62 | # that the docstring itself is NOT destroyed by the decorator. | |
82 | @skip_doctest |
|
63 | @skip_doctest | |
83 | def doctest_bad(x,y=1,**k): |
|
64 | def doctest_bad(x,y=1,**k): | |
84 | """A function whose doctest we need to skip. |
|
65 | """A function whose doctest we need to skip. | |
85 |
|
66 | |||
86 | >>> 1+1 |
|
67 | >>> 1+1 | |
87 | 3 |
|
68 | 3 | |
88 | """ |
|
69 | """ | |
89 | print 'x:',x |
|
70 | print 'x:',x | |
90 | print 'y:',y |
|
71 | print 'y:',y | |
91 | print 'k:',k |
|
72 | print 'k:',k | |
92 |
|
73 | |||
93 |
|
74 | |||
94 | def call_doctest_bad(): |
|
75 | def call_doctest_bad(): | |
95 | """Check that we can still call the decorated functions. |
|
76 | """Check that we can still call the decorated functions. | |
96 |
|
77 | |||
97 | >>> doctest_bad(3,y=4) |
|
78 | >>> doctest_bad(3,y=4) | |
98 | x: 3 |
|
79 | x: 3 | |
99 | y: 4 |
|
80 | y: 4 | |
100 | k: {} |
|
81 | k: {} | |
101 | """ |
|
82 | """ | |
102 | pass |
|
83 | pass | |
103 |
|
84 | |||
104 |
|
85 | |||
105 | def test_skip_dt_decorator(): |
|
86 | def test_skip_dt_decorator(): | |
106 | """Doctest-skipping decorator should preserve the docstring. |
|
87 | """Doctest-skipping decorator should preserve the docstring. | |
107 | """ |
|
88 | """ | |
108 | # Careful: 'check' must be a *verbatim* copy of the doctest_bad docstring! |
|
89 | # Careful: 'check' must be a *verbatim* copy of the doctest_bad docstring! | |
109 | check = """A function whose doctest we need to skip. |
|
90 | check = """A function whose doctest we need to skip. | |
110 |
|
91 | |||
111 | >>> 1+1 |
|
92 | >>> 1+1 | |
112 | 3 |
|
93 | 3 | |
113 | """ |
|
94 | """ | |
114 | # Fetch the docstring from doctest_bad after decoration. |
|
95 | # Fetch the docstring from doctest_bad after decoration. | |
115 | val = doctest_bad.__doc__ |
|
96 | val = doctest_bad.__doc__ | |
116 |
|
97 | |||
117 | nt.assert_equal(check,val,"doctest_bad docstrings don't match") |
|
98 | nt.assert_equal(check,val,"doctest_bad docstrings don't match") | |
118 |
|
99 | |||
119 |
|
100 | |||
120 | # Doctest skipping should work for class methods too |
|
101 | # Doctest skipping should work for class methods too | |
121 | class FooClass(object): |
|
102 | class FooClass(object): | |
122 | """FooClass |
|
103 | """FooClass | |
123 |
|
104 | |||
124 | Example: |
|
105 | Example: | |
125 |
|
106 | |||
126 | >>> 1+1 |
|
107 | >>> 1+1 | |
127 | 2 |
|
108 | 2 | |
128 | """ |
|
109 | """ | |
129 |
|
110 | |||
130 | @skip_doctest |
|
111 | @skip_doctest | |
131 | def __init__(self,x): |
|
112 | def __init__(self,x): | |
132 | """Make a FooClass. |
|
113 | """Make a FooClass. | |
133 |
|
114 | |||
134 | Example: |
|
115 | Example: | |
135 |
|
116 | |||
136 | >>> f = FooClass(3) |
|
117 | >>> f = FooClass(3) | |
137 | junk |
|
118 | junk | |
138 | """ |
|
119 | """ | |
139 | print 'Making a FooClass.' |
|
120 | print 'Making a FooClass.' | |
140 | self.x = x |
|
121 | self.x = x | |
141 |
|
122 | |||
142 | @skip_doctest |
|
123 | @skip_doctest | |
143 | def bar(self,y): |
|
124 | def bar(self,y): | |
144 | """Example: |
|
125 | """Example: | |
145 |
|
126 | |||
146 | >>> ff = FooClass(3) |
|
127 | >>> ff = FooClass(3) | |
147 | >>> ff.bar(0) |
|
128 | >>> ff.bar(0) | |
148 | boom! |
|
129 | boom! | |
149 | >>> 1/0 |
|
130 | >>> 1/0 | |
150 | bam! |
|
131 | bam! | |
151 | """ |
|
132 | """ | |
152 | return 1/y |
|
133 | return 1/y | |
153 |
|
134 | |||
154 | def baz(self,y): |
|
135 | def baz(self,y): | |
155 | """Example: |
|
136 | """Example: | |
156 |
|
137 | |||
157 | >>> ff2 = FooClass(3) |
|
138 | >>> ff2 = FooClass(3) | |
158 | Making a FooClass. |
|
139 | Making a FooClass. | |
159 | >>> ff2.baz(3) |
|
140 | >>> ff2.baz(3) | |
160 | True |
|
141 | True | |
161 | """ |
|
142 | """ | |
162 | return self.x==y |
|
143 | return self.x==y | |
163 |
|
144 | |||
164 |
|
145 | |||
165 | def test_skip_dt_decorator2(): |
|
146 | def test_skip_dt_decorator2(): | |
166 | """Doctest-skipping decorator should preserve function signature. |
|
147 | """Doctest-skipping decorator should preserve function signature. | |
167 | """ |
|
148 | """ | |
168 | # Hardcoded correct answer |
|
149 | # Hardcoded correct answer | |
169 | dtargs = (['x', 'y'], None, 'k', (1,)) |
|
150 | dtargs = (['x', 'y'], None, 'k', (1,)) | |
170 | # Introspect out the value |
|
151 | # Introspect out the value | |
171 | dtargsr = getargspec(doctest_bad) |
|
152 | dtargsr = getargspec(doctest_bad) | |
172 | assert dtargsr==dtargs, \ |
|
153 | assert dtargsr==dtargs, \ | |
173 | "Incorrectly reconstructed args for doctest_bad: %s" % (dtargsr,) |
|
154 | "Incorrectly reconstructed args for doctest_bad: %s" % (dtargsr,) | |
174 |
|
155 | |||
175 |
|
156 | |||
176 | @dec.skip_linux |
|
157 | @dec.skip_linux | |
177 | def test_linux(): |
|
158 | def test_linux(): | |
178 | nt.assert_false(sys.platform.startswith('linux'),"This test can't run under linux") |
|
159 | nt.assert_false(sys.platform.startswith('linux'),"This test can't run under linux") | |
179 |
|
160 | |||
180 | @dec.skip_win32 |
|
161 | @dec.skip_win32 | |
181 | def test_win32(): |
|
162 | def test_win32(): | |
182 | nt.assert_not_equal(sys.platform,'win32',"This test can't run under windows") |
|
163 | nt.assert_not_equal(sys.platform,'win32',"This test can't run under windows") | |
183 |
|
164 | |||
184 | @dec.skip_osx |
|
165 | @dec.skip_osx | |
185 | def test_osx(): |
|
166 | def test_osx(): | |
186 | nt.assert_not_equal(sys.platform,'darwin',"This test can't run under osx") |
|
167 | nt.assert_not_equal(sys.platform,'darwin',"This test can't run under osx") | |
187 |
|
168 |
@@ -1,135 +1,133 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | Tests for testing.tools |
|
3 | Tests for testing.tools | |
4 | """ |
|
4 | """ | |
5 |
|
5 | |||
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 | # Copyright (C) 2008-2011 The IPython Development Team |
|
7 | # Copyright (C) 2008-2011 The IPython Development Team | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | # Distributed under the terms of the BSD License. The full license is in | |
10 | # the file COPYING, distributed as part of this software. |
|
10 | # the file COPYING, distributed as part of this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | from __future__ import with_statement |
|
16 | from __future__ import with_statement | |
17 |
|
17 | |||
18 | import os |
|
18 | import os | |
19 | import unittest |
|
19 | import unittest | |
20 |
|
20 | |||
21 | import nose.tools as nt |
|
21 | import nose.tools as nt | |
22 |
|
22 | |||
23 | from IPython.testing import decorators as dec |
|
23 | from IPython.testing import decorators as dec | |
24 | from IPython.testing import tools as tt |
|
24 | from IPython.testing import tools as tt | |
25 |
|
25 | |||
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 | # Tests |
|
27 | # Tests | |
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 |
|
29 | |||
30 | @dec.skip_win32 |
|
30 | @dec.skip_win32 | |
31 | def test_full_path_posix(): |
|
31 | def test_full_path_posix(): | |
32 | spath = '/foo/bar.py' |
|
32 | spath = '/foo/bar.py' | |
33 | result = tt.full_path(spath,['a.txt','b.txt']) |
|
33 | result = tt.full_path(spath,['a.txt','b.txt']) | |
34 | nt.assert_equal(result, ['/foo/a.txt', '/foo/b.txt']) |
|
34 | nt.assert_equal(result, ['/foo/a.txt', '/foo/b.txt']) | |
35 | spath = '/foo' |
|
35 | spath = '/foo' | |
36 | result = tt.full_path(spath,['a.txt','b.txt']) |
|
36 | result = tt.full_path(spath,['a.txt','b.txt']) | |
37 | nt.assert_equal(result, ['/a.txt', '/b.txt']) |
|
37 | nt.assert_equal(result, ['/a.txt', '/b.txt']) | |
38 | result = tt.full_path(spath,'a.txt') |
|
38 | result = tt.full_path(spath,'a.txt') | |
39 | nt.assert_equal(result, ['/a.txt']) |
|
39 | nt.assert_equal(result, ['/a.txt']) | |
40 |
|
40 | |||
41 |
|
41 | |||
42 | @dec.skip_if_not_win32 |
|
42 | @dec.skip_if_not_win32 | |
43 | def test_full_path_win32(): |
|
43 | def test_full_path_win32(): | |
44 | spath = 'c:\\foo\\bar.py' |
|
44 | spath = 'c:\\foo\\bar.py' | |
45 | result = tt.full_path(spath,['a.txt','b.txt']) |
|
45 | result = tt.full_path(spath,['a.txt','b.txt']) | |
46 | nt.assert_equal(result, ['c:\\foo\\a.txt', 'c:\\foo\\b.txt']) |
|
46 | nt.assert_equal(result, ['c:\\foo\\a.txt', 'c:\\foo\\b.txt']) | |
47 | spath = 'c:\\foo' |
|
47 | spath = 'c:\\foo' | |
48 | result = tt.full_path(spath,['a.txt','b.txt']) |
|
48 | result = tt.full_path(spath,['a.txt','b.txt']) | |
49 | nt.assert_equal(result, ['c:\\a.txt', 'c:\\b.txt']) |
|
49 | nt.assert_equal(result, ['c:\\a.txt', 'c:\\b.txt']) | |
50 | result = tt.full_path(spath,'a.txt') |
|
50 | result = tt.full_path(spath,'a.txt') | |
51 | nt.assert_equal(result, ['c:\\a.txt']) |
|
51 | nt.assert_equal(result, ['c:\\a.txt']) | |
52 |
|
52 | |||
53 |
|
53 | |||
54 | @dec.parametric |
|
|||
55 | def test_parser(): |
|
54 | def test_parser(): | |
56 | err = ("FAILED (errors=1)", 1, 0) |
|
55 | err = ("FAILED (errors=1)", 1, 0) | |
57 | fail = ("FAILED (failures=1)", 0, 1) |
|
56 | fail = ("FAILED (failures=1)", 0, 1) | |
58 | both = ("FAILED (errors=1, failures=1)", 1, 1) |
|
57 | both = ("FAILED (errors=1, failures=1)", 1, 1) | |
59 | for txt, nerr, nfail in [err, fail, both]: |
|
58 | for txt, nerr, nfail in [err, fail, both]: | |
60 | nerr1, nfail1 = tt.parse_test_output(txt) |
|
59 | nerr1, nfail1 = tt.parse_test_output(txt) | |
61 |
|
|
60 | nt.assert_equal(nerr, nerr1) | |
62 |
|
|
61 | nt.assert_equal(nfail, nfail1) | |
63 |
|
62 | |||
64 |
|
63 | |||
65 | @dec.parametric |
|
|||
66 | def test_temp_pyfile(): |
|
64 | def test_temp_pyfile(): | |
67 | src = 'pass\n' |
|
65 | src = 'pass\n' | |
68 | fname, fh = tt.temp_pyfile(src) |
|
66 | fname, fh = tt.temp_pyfile(src) | |
69 |
|
|
67 | assert os.path.isfile(fname) | |
70 | fh.close() |
|
68 | fh.close() | |
71 | with open(fname) as fh2: |
|
69 | with open(fname) as fh2: | |
72 | src2 = fh2.read() |
|
70 | src2 = fh2.read() | |
73 |
|
|
71 | nt.assert_equal(src2, src) | |
74 |
|
72 | |||
75 | class TestAssertPrints(unittest.TestCase): |
|
73 | class TestAssertPrints(unittest.TestCase): | |
76 | def test_passing(self): |
|
74 | def test_passing(self): | |
77 | with tt.AssertPrints("abc"): |
|
75 | with tt.AssertPrints("abc"): | |
78 | print "abcd" |
|
76 | print "abcd" | |
79 | print "def" |
|
77 | print "def" | |
80 | print b"ghi" |
|
78 | print b"ghi" | |
81 |
|
79 | |||
82 | def test_failing(self): |
|
80 | def test_failing(self): | |
83 | def func(): |
|
81 | def func(): | |
84 | with tt.AssertPrints("abc"): |
|
82 | with tt.AssertPrints("abc"): | |
85 | print "acd" |
|
83 | print "acd" | |
86 | print "def" |
|
84 | print "def" | |
87 | print b"ghi" |
|
85 | print b"ghi" | |
88 |
|
86 | |||
89 | self.assertRaises(AssertionError, func) |
|
87 | self.assertRaises(AssertionError, func) | |
90 |
|
88 | |||
91 |
|
89 | |||
92 | class Test_ipexec_validate(unittest.TestCase, tt.TempFileMixin): |
|
90 | class Test_ipexec_validate(unittest.TestCase, tt.TempFileMixin): | |
93 | def test_main_path(self): |
|
91 | def test_main_path(self): | |
94 | """Test with only stdout results. |
|
92 | """Test with only stdout results. | |
95 | """ |
|
93 | """ | |
96 | self.mktmp("print('A')\n" |
|
94 | self.mktmp("print('A')\n" | |
97 | "print('B')\n" |
|
95 | "print('B')\n" | |
98 | ) |
|
96 | ) | |
99 | out = "A\nB" |
|
97 | out = "A\nB" | |
100 | tt.ipexec_validate(self.fname, out) |
|
98 | tt.ipexec_validate(self.fname, out) | |
101 |
|
99 | |||
102 | def test_main_path2(self): |
|
100 | def test_main_path2(self): | |
103 | """Test with only stdout results, expecting windows line endings. |
|
101 | """Test with only stdout results, expecting windows line endings. | |
104 | """ |
|
102 | """ | |
105 | self.mktmp("print('A')\n" |
|
103 | self.mktmp("print('A')\n" | |
106 | "print('B')\n" |
|
104 | "print('B')\n" | |
107 | ) |
|
105 | ) | |
108 | out = "A\r\nB" |
|
106 | out = "A\r\nB" | |
109 | tt.ipexec_validate(self.fname, out) |
|
107 | tt.ipexec_validate(self.fname, out) | |
110 |
|
108 | |||
111 | def test_exception_path(self): |
|
109 | def test_exception_path(self): | |
112 | """Test exception path in exception_validate. |
|
110 | """Test exception path in exception_validate. | |
113 | """ |
|
111 | """ | |
114 | self.mktmp("from __future__ import print_function\n" |
|
112 | self.mktmp("from __future__ import print_function\n" | |
115 | "import sys\n" |
|
113 | "import sys\n" | |
116 | "print('A')\n" |
|
114 | "print('A')\n" | |
117 | "print('B')\n" |
|
115 | "print('B')\n" | |
118 | "print('C', file=sys.stderr)\n" |
|
116 | "print('C', file=sys.stderr)\n" | |
119 | "print('D', file=sys.stderr)\n" |
|
117 | "print('D', file=sys.stderr)\n" | |
120 | ) |
|
118 | ) | |
121 | out = "A\nB" |
|
119 | out = "A\nB" | |
122 | tt.ipexec_validate(self.fname, expected_out=out, expected_err="C\nD") |
|
120 | tt.ipexec_validate(self.fname, expected_out=out, expected_err="C\nD") | |
123 |
|
121 | |||
124 | def test_exception_path2(self): |
|
122 | def test_exception_path2(self): | |
125 | """Test exception path in exception_validate, expecting windows line endings. |
|
123 | """Test exception path in exception_validate, expecting windows line endings. | |
126 | """ |
|
124 | """ | |
127 | self.mktmp("from __future__ import print_function\n" |
|
125 | self.mktmp("from __future__ import print_function\n" | |
128 | "import sys\n" |
|
126 | "import sys\n" | |
129 | "print('A')\n" |
|
127 | "print('A')\n" | |
130 | "print('B')\n" |
|
128 | "print('B')\n" | |
131 | "print('C', file=sys.stderr)\n" |
|
129 | "print('C', file=sys.stderr)\n" | |
132 | "print('D', file=sys.stderr)\n" |
|
130 | "print('D', file=sys.stderr)\n" | |
133 | ) |
|
131 | ) | |
134 | out = "A\r\nB" |
|
132 | out = "A\r\nB" | |
135 | tt.ipexec_validate(self.fname, expected_out=out, expected_err="C\r\nD") |
|
133 | tt.ipexec_validate(self.fname, expected_out=out, expected_err="C\r\nD") |
General Comments 0
You need to be logged in to leave comments.
Login now