##// END OF EJS Templates
[testing][tests][decorators] Remove nose
Samuel Gaist -
Show More
@@ -1,164 +1,162 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
10 import nose.tools as nt
11
12 # Our own
9 # Our own
13 from IPython.testing import decorators as dec
10 from IPython.testing import decorators as dec
14
11
15 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
16 # Utilities
13 # Utilities
17
14
18 # Note: copied from OInspect, kept here so the testing stuff doesn't create
15 # Note: copied from OInspect, kept here so the testing stuff doesn't create
19 # circular dependencies and is easier to reuse.
16 # circular dependencies and is easier to reuse.
20 def getargspec(obj):
17 def getargspec(obj):
21 """Get the names and default values of a function's arguments.
18 """Get the names and default values of a function's arguments.
22
19
23 A tuple of four things is returned: (args, varargs, varkw, defaults).
20 A tuple of four things is returned: (args, varargs, varkw, defaults).
24 'args' is a list of the argument names (it may contain nested lists).
21 'args' is a list of the argument names (it may contain nested lists).
25 'varargs' and 'varkw' are the names of the * and ** arguments or None.
22 'varargs' and 'varkw' are the names of the * and ** arguments or None.
26 'defaults' is an n-tuple of the default values of the last n arguments.
23 'defaults' is an n-tuple of the default values of the last n arguments.
27
24
28 Modified version of inspect.getargspec from the Python Standard
25 Modified version of inspect.getargspec from the Python Standard
29 Library."""
26 Library."""
30
27
31 if inspect.isfunction(obj):
28 if inspect.isfunction(obj):
32 func_obj = obj
29 func_obj = obj
33 elif inspect.ismethod(obj):
30 elif inspect.ismethod(obj):
34 func_obj = obj.__func__
31 func_obj = obj.__func__
35 else:
32 else:
36 raise TypeError('arg is not a Python function')
33 raise TypeError('arg is not a Python function')
37 args, varargs, varkw = inspect.getargs(func_obj.__code__)
34 args, varargs, varkw = inspect.getargs(func_obj.__code__)
38 return args, varargs, varkw, func_obj.__defaults__
35 return args, varargs, varkw, func_obj.__defaults__
39
36
40 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
41 # Testing functions
38 # Testing functions
42
39
43 @dec.as_unittest
40 @dec.as_unittest
44 def trivial():
41 def trivial():
45 """A trivial test"""
42 """A trivial test"""
46 pass
43 pass
47
44
48
45
49 @dec.skip()
46 @dec.skip()
50 def test_deliberately_broken():
47 def test_deliberately_broken():
51 """A deliberately broken test - we want to skip this one."""
48 """A deliberately broken test - we want to skip this one."""
52 1/0
49 1/0
53
50
54 @dec.skip('Testing the skip decorator')
51 @dec.skip('Testing the skip decorator')
55 def test_deliberately_broken2():
52 def test_deliberately_broken2():
56 """Another deliberately broken test - we want to skip this one."""
53 """Another deliberately broken test - we want to skip this one."""
57 1/0
54 1/0
58
55
59
56
60 # Verify that we can correctly skip the doctest for a function at will, but
57 # Verify that we can correctly skip the doctest for a function at will, but
61 # that the docstring itself is NOT destroyed by the decorator.
58 # that the docstring itself is NOT destroyed by the decorator.
62 def doctest_bad(x,y=1,**k):
59 def doctest_bad(x,y=1,**k):
63 """A function whose doctest we need to skip.
60 """A function whose doctest we need to skip.
64
61
65 >>> 1+1
62 >>> 1+1
66 3
63 3
67 """
64 """
68 print('x:',x)
65 print('x:',x)
69 print('y:',y)
66 print('y:',y)
70 print('k:',k)
67 print('k:',k)
71
68
72
69
73 def call_doctest_bad():
70 def call_doctest_bad():
74 """Check that we can still call the decorated functions.
71 """Check that we can still call the decorated functions.
75
72
76 >>> doctest_bad(3,y=4)
73 >>> doctest_bad(3,y=4)
77 x: 3
74 x: 3
78 y: 4
75 y: 4
79 k: {}
76 k: {}
80 """
77 """
81 pass
78 pass
82
79
83
80
84 def test_skip_dt_decorator():
81 def test_skip_dt_decorator():
85 """Doctest-skipping decorator should preserve the docstring.
82 """Doctest-skipping decorator should preserve the docstring.
86 """
83 """
87 # Careful: 'check' must be a *verbatim* copy of the doctest_bad docstring!
84 # Careful: 'check' must be a *verbatim* copy of the doctest_bad docstring!
88 check = """A function whose doctest we need to skip.
85 check = """A function whose doctest we need to skip.
89
86
90 >>> 1+1
87 >>> 1+1
91 3
88 3
92 """
89 """
93 # Fetch the docstring from doctest_bad after decoration.
90 # Fetch the docstring from doctest_bad after decoration.
94 val = doctest_bad.__doc__
91 val = doctest_bad.__doc__
95
92
96 nt.assert_equal(check,val,"doctest_bad docstrings don't match")
93 assert check == val, "doctest_bad docstrings don't match"
97
94
98
95
99 # Doctest skipping should work for class methods too
96 # Doctest skipping should work for class methods too
100 class FooClass(object):
97 class FooClass(object):
101 """FooClass
98 """FooClass
102
99
103 Example:
100 Example:
104
101
105 >>> 1+1
102 >>> 1+1
106 2
103 2
107 """
104 """
108
105
109 def __init__(self,x):
106 def __init__(self,x):
110 """Make a FooClass.
107 """Make a FooClass.
111
108
112 Example:
109 Example:
113
110
114 >>> f = FooClass(3)
111 >>> f = FooClass(3)
115 junk
112 junk
116 """
113 """
117 print('Making a FooClass.')
114 print('Making a FooClass.')
118 self.x = x
115 self.x = x
119
116
120 def bar(self,y):
117 def bar(self,y):
121 """Example:
118 """Example:
122
119
123 >>> ff = FooClass(3)
120 >>> ff = FooClass(3)
124 >>> ff.bar(0)
121 >>> ff.bar(0)
125 boom!
122 boom!
126 >>> 1/0
123 >>> 1/0
127 bam!
124 bam!
128 """
125 """
129 return 1/y
126 return 1/y
130
127
131 def baz(self,y):
128 def baz(self,y):
132 """Example:
129 """Example:
133
130
134 >>> ff2 = FooClass(3)
131 >>> ff2 = FooClass(3)
135 Making a FooClass.
132 Making a FooClass.
136 >>> ff2.baz(3)
133 >>> ff2.baz(3)
137 True
134 True
138 """
135 """
139 return self.x==y
136 return self.x==y
140
137
141
138
142 def test_skip_dt_decorator2():
139 def test_skip_dt_decorator2():
143 """Doctest-skipping decorator should preserve function signature.
140 """Doctest-skipping decorator should preserve function signature.
144 """
141 """
145 # Hardcoded correct answer
142 # Hardcoded correct answer
146 dtargs = (['x', 'y'], None, 'k', (1,))
143 dtargs = (['x', 'y'], None, 'k', (1,))
147 # Introspect out the value
144 # Introspect out the value
148 dtargsr = getargspec(doctest_bad)
145 dtargsr = getargspec(doctest_bad)
149 assert dtargsr==dtargs, \
146 assert dtargsr==dtargs, \
150 "Incorrectly reconstructed args for doctest_bad: %s" % (dtargsr,)
147 "Incorrectly reconstructed args for doctest_bad: %s" % (dtargsr,)
151
148
152
149
153 @dec.skip_linux
150 @dec.skip_linux
154 def test_linux():
151 def test_linux():
155 nt.assert_false(sys.platform.startswith('linux'),"This test can't run under linux")
152 assert sys.platform.startswith("linux") is False, "This test can't run under linux"
153
156
154
157 @dec.skip_win32
155 @dec.skip_win32
158 def test_win32():
156 def test_win32():
159 nt.assert_not_equal(sys.platform,'win32',"This test can't run under windows")
157 assert sys.platform != "win32", "This test can't run under windows"
158
160
159
161 @dec.skip_osx
160 @dec.skip_osx
162 def test_osx():
161 def test_osx():
163 nt.assert_not_equal(sys.platform,'darwin',"This test can't run under osx")
162 assert sys.platform != "darwin", "This test can't run under osx"
164
General Comments 0
You need to be logged in to leave comments. Login now