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