##// END OF EJS Templates
Use better temp file for onlyif_unicode_paths test.
Thomas Kluyver -
Show More
@@ -1,333 +1,335 b''
1 # -*- coding: utf-8 -*-
1 """Decorators for labeling test objects.
2 """Decorators for labeling test objects.
2
3
3 Decorators that merely return a modified version of the original function
4 Decorators that merely return a modified version of the original function
4 object are straightforward. Decorators that return a new function object need
5 object are straightforward. Decorators that return a new function object need
5 to use nose.tools.make_decorator(original_function)(decorator) in returning the
6 to use nose.tools.make_decorator(original_function)(decorator) in returning the
6 decorator, in order to preserve metadata such as function name, setup and
7 decorator, in order to preserve metadata such as function name, setup and
7 teardown functions and so on - see nose.tools for more information.
8 teardown functions and so on - see nose.tools for more information.
8
9
9 This module provides a set of useful decorators meant to be ready to use in
10 This module provides a set of useful decorators meant to be ready to use in
10 your own tests. See the bottom of the file for the ready-made ones, and if you
11 your own tests. See the bottom of the file for the ready-made ones, and if you
11 find yourself writing a new one that may be of generic use, add it here.
12 find yourself writing a new one that may be of generic use, add it here.
12
13
13 Included decorators:
14 Included decorators:
14
15
15
16
16 Lightweight testing that remains unittest-compatible.
17 Lightweight testing that remains unittest-compatible.
17
18
18 - @parametric, for parametric test support that is vastly easier to use than
19 - @parametric, for parametric test support that is vastly easier to use than
19 nose's for debugging. With ours, if a test fails, the stack under inspection
20 nose's for debugging. With ours, if a test fails, the stack under inspection
20 is that of the test and not that of the test framework.
21 is that of the test and not that of the test framework.
21
22
22 - An @as_unittest decorator can be used to tag any normal parameter-less
23 - An @as_unittest decorator can be used to tag any normal parameter-less
23 function as a unittest TestCase. Then, both nose and normal unittest will
24 function as a unittest TestCase. Then, both nose and normal unittest will
24 recognize it as such. This will make it easier to migrate away from Nose if
25 recognize it as such. This will make it easier to migrate away from Nose if
25 we ever need/want to while maintaining very lightweight tests.
26 we ever need/want to while maintaining very lightweight tests.
26
27
27 NOTE: This file contains IPython-specific decorators. Using the machinery in
28 NOTE: This file contains IPython-specific decorators. Using the machinery in
28 IPython.external.decorators, we import either numpy.testing.decorators if numpy is
29 IPython.external.decorators, we import either numpy.testing.decorators if numpy is
29 available, OR use equivalent code in IPython.external._decorators, which
30 available, OR use equivalent code in IPython.external._decorators, which
30 we've copied verbatim from numpy.
31 we've copied verbatim from numpy.
31
32
32 Authors
33 Authors
33 -------
34 -------
34
35
35 - Fernando Perez <Fernando.Perez@berkeley.edu>
36 - Fernando Perez <Fernando.Perez@berkeley.edu>
36 """
37 """
37
38
38 #-----------------------------------------------------------------------------
39 #-----------------------------------------------------------------------------
39 # Copyright (C) 2009-2010 The IPython Development Team
40 # Copyright (C) 2009-2010 The IPython Development Team
40 #
41 #
41 # Distributed under the terms of the BSD License. The full license is in
42 # Distributed under the terms of the BSD License. The full license is in
42 # the file COPYING, distributed as part of this software.
43 # the file COPYING, distributed as part of this software.
43 #-----------------------------------------------------------------------------
44 #-----------------------------------------------------------------------------
44
45
45 #-----------------------------------------------------------------------------
46 #-----------------------------------------------------------------------------
46 # Imports
47 # Imports
47 #-----------------------------------------------------------------------------
48 #-----------------------------------------------------------------------------
48
49
49 # Stdlib imports
50 # Stdlib imports
50 import inspect
51 import inspect
51 import sys
52 import sys
52 import tempfile
53 import tempfile
53 import unittest
54 import unittest
54
55
55 # Third-party imports
56 # Third-party imports
56
57
57 # This is Michele Simionato's decorator module, kept verbatim.
58 # This is Michele Simionato's decorator module, kept verbatim.
58 from IPython.external.decorator import decorator, update_wrapper
59 from IPython.external.decorator import decorator, update_wrapper
59
60
60 # We already have python3-compliant code for parametric tests
61 # We already have python3-compliant code for parametric tests
61 if sys.version[0]=='2':
62 if sys.version[0]=='2':
62 from _paramtestpy2 import parametric, ParametricTestCase
63 from _paramtestpy2 import parametric, ParametricTestCase
63 else:
64 else:
64 from _paramtestpy3 import parametric, ParametricTestCase
65 from _paramtestpy3 import parametric, ParametricTestCase
65
66
66 # Expose the unittest-driven decorators
67 # Expose the unittest-driven decorators
67 from ipunittest import ipdoctest, ipdocstring
68 from ipunittest import ipdoctest, ipdocstring
68
69
69 # Grab the numpy-specific decorators which we keep in a file that we
70 # Grab the numpy-specific decorators which we keep in a file that we
70 # occasionally update from upstream: decorators.py is a copy of
71 # occasionally update from upstream: decorators.py is a copy of
71 # numpy.testing.decorators, we expose all of it here.
72 # numpy.testing.decorators, we expose all of it here.
72 from IPython.external.decorators import *
73 from IPython.external.decorators import *
73
74
74 #-----------------------------------------------------------------------------
75 #-----------------------------------------------------------------------------
75 # Classes and functions
76 # Classes and functions
76 #-----------------------------------------------------------------------------
77 #-----------------------------------------------------------------------------
77
78
78 # Simple example of the basic idea
79 # Simple example of the basic idea
79 def as_unittest(func):
80 def as_unittest(func):
80 """Decorator to make a simple function into a normal test via unittest."""
81 """Decorator to make a simple function into a normal test via unittest."""
81 class Tester(unittest.TestCase):
82 class Tester(unittest.TestCase):
82 def test(self):
83 def test(self):
83 func()
84 func()
84
85
85 Tester.__name__ = func.__name__
86 Tester.__name__ = func.__name__
86
87
87 return Tester
88 return Tester
88
89
89 # Utility functions
90 # Utility functions
90
91
91 def apply_wrapper(wrapper,func):
92 def apply_wrapper(wrapper,func):
92 """Apply a wrapper to a function for decoration.
93 """Apply a wrapper to a function for decoration.
93
94
94 This mixes Michele Simionato's decorator tool with nose's make_decorator,
95 This mixes Michele Simionato's decorator tool with nose's make_decorator,
95 to apply a wrapper in a decorator so that all nose attributes, as well as
96 to apply a wrapper in a decorator so that all nose attributes, as well as
96 function signature and other properties, survive the decoration cleanly.
97 function signature and other properties, survive the decoration cleanly.
97 This will ensure that wrapped functions can still be well introspected via
98 This will ensure that wrapped functions can still be well introspected via
98 IPython, for example.
99 IPython, for example.
99 """
100 """
100 import nose.tools
101 import nose.tools
101
102
102 return decorator(wrapper,nose.tools.make_decorator(func)(wrapper))
103 return decorator(wrapper,nose.tools.make_decorator(func)(wrapper))
103
104
104
105
105 def make_label_dec(label,ds=None):
106 def make_label_dec(label,ds=None):
106 """Factory function to create a decorator that applies one or more labels.
107 """Factory function to create a decorator that applies one or more labels.
107
108
108 Parameters
109 Parameters
109 ----------
110 ----------
110 label : string or sequence
111 label : string or sequence
111 One or more labels that will be applied by the decorator to the functions
112 One or more labels that will be applied by the decorator to the functions
112 it decorates. Labels are attributes of the decorated function with their
113 it decorates. Labels are attributes of the decorated function with their
113 value set to True.
114 value set to True.
114
115
115 ds : string
116 ds : string
116 An optional docstring for the resulting decorator. If not given, a
117 An optional docstring for the resulting decorator. If not given, a
117 default docstring is auto-generated.
118 default docstring is auto-generated.
118
119
119 Returns
120 Returns
120 -------
121 -------
121 A decorator.
122 A decorator.
122
123
123 Examples
124 Examples
124 --------
125 --------
125
126
126 A simple labeling decorator:
127 A simple labeling decorator:
127 >>> slow = make_label_dec('slow')
128 >>> slow = make_label_dec('slow')
128 >>> print slow.__doc__
129 >>> print slow.__doc__
129 Labels a test as 'slow'.
130 Labels a test as 'slow'.
130
131
131 And one that uses multiple labels and a custom docstring:
132 And one that uses multiple labels and a custom docstring:
132 >>> rare = make_label_dec(['slow','hard'],
133 >>> rare = make_label_dec(['slow','hard'],
133 ... "Mix labels 'slow' and 'hard' for rare tests.")
134 ... "Mix labels 'slow' and 'hard' for rare tests.")
134 >>> print rare.__doc__
135 >>> print rare.__doc__
135 Mix labels 'slow' and 'hard' for rare tests.
136 Mix labels 'slow' and 'hard' for rare tests.
136
137
137 Now, let's test using this one:
138 Now, let's test using this one:
138 >>> @rare
139 >>> @rare
139 ... def f(): pass
140 ... def f(): pass
140 ...
141 ...
141 >>>
142 >>>
142 >>> f.slow
143 >>> f.slow
143 True
144 True
144 >>> f.hard
145 >>> f.hard
145 True
146 True
146 """
147 """
147
148
148 if isinstance(label,basestring):
149 if isinstance(label,basestring):
149 labels = [label]
150 labels = [label]
150 else:
151 else:
151 labels = label
152 labels = label
152
153
153 # Validate that the given label(s) are OK for use in setattr() by doing a
154 # Validate that the given label(s) are OK for use in setattr() by doing a
154 # dry run on a dummy function.
155 # dry run on a dummy function.
155 tmp = lambda : None
156 tmp = lambda : None
156 for label in labels:
157 for label in labels:
157 setattr(tmp,label,True)
158 setattr(tmp,label,True)
158
159
159 # This is the actual decorator we'll return
160 # This is the actual decorator we'll return
160 def decor(f):
161 def decor(f):
161 for label in labels:
162 for label in labels:
162 setattr(f,label,True)
163 setattr(f,label,True)
163 return f
164 return f
164
165
165 # Apply the user's docstring, or autogenerate a basic one
166 # Apply the user's docstring, or autogenerate a basic one
166 if ds is None:
167 if ds is None:
167 ds = "Labels a test as %r." % label
168 ds = "Labels a test as %r." % label
168 decor.__doc__ = ds
169 decor.__doc__ = ds
169
170
170 return decor
171 return decor
171
172
172
173
173 # Inspired by numpy's skipif, but uses the full apply_wrapper utility to
174 # Inspired by numpy's skipif, but uses the full apply_wrapper utility to
174 # preserve function metadata better and allows the skip condition to be a
175 # preserve function metadata better and allows the skip condition to be a
175 # callable.
176 # callable.
176 def skipif(skip_condition, msg=None):
177 def skipif(skip_condition, msg=None):
177 ''' Make function raise SkipTest exception if skip_condition is true
178 ''' Make function raise SkipTest exception if skip_condition is true
178
179
179 Parameters
180 Parameters
180 ----------
181 ----------
181 skip_condition : bool or callable.
182 skip_condition : bool or callable.
182 Flag to determine whether to skip test. If the condition is a
183 Flag to determine whether to skip test. If the condition is a
183 callable, it is used at runtime to dynamically make the decision. This
184 callable, it is used at runtime to dynamically make the decision. This
184 is useful for tests that may require costly imports, to delay the cost
185 is useful for tests that may require costly imports, to delay the cost
185 until the test suite is actually executed.
186 until the test suite is actually executed.
186 msg : string
187 msg : string
187 Message to give on raising a SkipTest exception
188 Message to give on raising a SkipTest exception
188
189
189 Returns
190 Returns
190 -------
191 -------
191 decorator : function
192 decorator : function
192 Decorator, which, when applied to a function, causes SkipTest
193 Decorator, which, when applied to a function, causes SkipTest
193 to be raised when the skip_condition was True, and the function
194 to be raised when the skip_condition was True, and the function
194 to be called normally otherwise.
195 to be called normally otherwise.
195
196
196 Notes
197 Notes
197 -----
198 -----
198 You will see from the code that we had to further decorate the
199 You will see from the code that we had to further decorate the
199 decorator with the nose.tools.make_decorator function in order to
200 decorator with the nose.tools.make_decorator function in order to
200 transmit function name, and various other metadata.
201 transmit function name, and various other metadata.
201 '''
202 '''
202
203
203 def skip_decorator(f):
204 def skip_decorator(f):
204 # Local import to avoid a hard nose dependency and only incur the
205 # Local import to avoid a hard nose dependency and only incur the
205 # import time overhead at actual test-time.
206 # import time overhead at actual test-time.
206 import nose
207 import nose
207
208
208 # Allow for both boolean or callable skip conditions.
209 # Allow for both boolean or callable skip conditions.
209 if callable(skip_condition):
210 if callable(skip_condition):
210 skip_val = skip_condition
211 skip_val = skip_condition
211 else:
212 else:
212 skip_val = lambda : skip_condition
213 skip_val = lambda : skip_condition
213
214
214 def get_msg(func,msg=None):
215 def get_msg(func,msg=None):
215 """Skip message with information about function being skipped."""
216 """Skip message with information about function being skipped."""
216 if msg is None: out = 'Test skipped due to test condition.'
217 if msg is None: out = 'Test skipped due to test condition.'
217 else: out = msg
218 else: out = msg
218 return "Skipping test: %s. %s" % (func.__name__,out)
219 return "Skipping test: %s. %s" % (func.__name__,out)
219
220
220 # We need to define *two* skippers because Python doesn't allow both
221 # We need to define *two* skippers because Python doesn't allow both
221 # return with value and yield inside the same function.
222 # return with value and yield inside the same function.
222 def skipper_func(*args, **kwargs):
223 def skipper_func(*args, **kwargs):
223 """Skipper for normal test functions."""
224 """Skipper for normal test functions."""
224 if skip_val():
225 if skip_val():
225 raise nose.SkipTest(get_msg(f,msg))
226 raise nose.SkipTest(get_msg(f,msg))
226 else:
227 else:
227 return f(*args, **kwargs)
228 return f(*args, **kwargs)
228
229
229 def skipper_gen(*args, **kwargs):
230 def skipper_gen(*args, **kwargs):
230 """Skipper for test generators."""
231 """Skipper for test generators."""
231 if skip_val():
232 if skip_val():
232 raise nose.SkipTest(get_msg(f,msg))
233 raise nose.SkipTest(get_msg(f,msg))
233 else:
234 else:
234 for x in f(*args, **kwargs):
235 for x in f(*args, **kwargs):
235 yield x
236 yield x
236
237
237 # Choose the right skipper to use when building the actual generator.
238 # Choose the right skipper to use when building the actual generator.
238 if nose.util.isgenerator(f):
239 if nose.util.isgenerator(f):
239 skipper = skipper_gen
240 skipper = skipper_gen
240 else:
241 else:
241 skipper = skipper_func
242 skipper = skipper_func
242
243
243 return nose.tools.make_decorator(f)(skipper)
244 return nose.tools.make_decorator(f)(skipper)
244
245
245 return skip_decorator
246 return skip_decorator
246
247
247 # A version with the condition set to true, common case just to attacha message
248 # A version with the condition set to true, common case just to attacha message
248 # to a skip decorator
249 # to a skip decorator
249 def skip(msg=None):
250 def skip(msg=None):
250 """Decorator factory - mark a test function for skipping from test suite.
251 """Decorator factory - mark a test function for skipping from test suite.
251
252
252 Parameters
253 Parameters
253 ----------
254 ----------
254 msg : string
255 msg : string
255 Optional message to be added.
256 Optional message to be added.
256
257
257 Returns
258 Returns
258 -------
259 -------
259 decorator : function
260 decorator : function
260 Decorator, which, when applied to a function, causes SkipTest
261 Decorator, which, when applied to a function, causes SkipTest
261 to be raised, with the optional message added.
262 to be raised, with the optional message added.
262 """
263 """
263
264
264 return skipif(True,msg)
265 return skipif(True,msg)
265
266
266
267
267 def onlyif(condition, msg):
268 def onlyif(condition, msg):
268 """The reverse from skipif, see skipif for details."""
269 """The reverse from skipif, see skipif for details."""
269
270
270 if callable(condition):
271 if callable(condition):
271 skip_condition = lambda : not condition()
272 skip_condition = lambda : not condition()
272 else:
273 else:
273 skip_condition = lambda : not condition
274 skip_condition = lambda : not condition
274
275
275 return skipif(skip_condition, msg)
276 return skipif(skip_condition, msg)
276
277
277 #-----------------------------------------------------------------------------
278 #-----------------------------------------------------------------------------
278 # Utility functions for decorators
279 # Utility functions for decorators
279 def module_not_available(module):
280 def module_not_available(module):
280 """Can module be imported? Returns true if module does NOT import.
281 """Can module be imported? Returns true if module does NOT import.
281
282
282 This is used to make a decorator to skip tests that require module to be
283 This is used to make a decorator to skip tests that require module to be
283 available, but delay the 'import numpy' to test execution time.
284 available, but delay the 'import numpy' to test execution time.
284 """
285 """
285 try:
286 try:
286 mod = __import__(module)
287 mod = __import__(module)
287 mod_not_avail = False
288 mod_not_avail = False
288 except ImportError:
289 except ImportError:
289 mod_not_avail = True
290 mod_not_avail = True
290
291
291 return mod_not_avail
292 return mod_not_avail
292
293
293 #-----------------------------------------------------------------------------
294 #-----------------------------------------------------------------------------
294 # Decorators for public use
295 # Decorators for public use
295
296
296 # Decorators to skip certain tests on specific platforms.
297 # Decorators to skip certain tests on specific platforms.
297 skip_win32 = skipif(sys.platform == 'win32',
298 skip_win32 = skipif(sys.platform == 'win32',
298 "This test does not run under Windows")
299 "This test does not run under Windows")
299 skip_linux = skipif(sys.platform == 'linux2',
300 skip_linux = skipif(sys.platform == 'linux2',
300 "This test does not run under Linux")
301 "This test does not run under Linux")
301 skip_osx = skipif(sys.platform == 'darwin',"This test does not run under OS X")
302 skip_osx = skipif(sys.platform == 'darwin',"This test does not run under OS X")
302
303
303
304
304 # Decorators to skip tests if not on specific platforms.
305 # Decorators to skip tests if not on specific platforms.
305 skip_if_not_win32 = skipif(sys.platform != 'win32',
306 skip_if_not_win32 = skipif(sys.platform != 'win32',
306 "This test only runs under Windows")
307 "This test only runs under Windows")
307 skip_if_not_linux = skipif(sys.platform != 'linux2',
308 skip_if_not_linux = skipif(sys.platform != 'linux2',
308 "This test only runs under Linux")
309 "This test only runs under Linux")
309 skip_if_not_osx = skipif(sys.platform != 'darwin',
310 skip_if_not_osx = skipif(sys.platform != 'darwin',
310 "This test only runs under OSX")
311 "This test only runs under OSX")
311
312
312 # Other skip decorators
313 # Other skip decorators
313 skipif_not_numpy = skipif(module_not_available('numpy'),"This test requires numpy")
314 skipif_not_numpy = skipif(module_not_available('numpy'),"This test requires numpy")
314
315
315 skipif_not_sympy = skipif(module_not_available('sympy'),"This test requires sympy")
316 skipif_not_sympy = skipif(module_not_available('sympy'),"This test requires sympy")
316
317
317 skip_known_failure = knownfailureif(True,'This test is known to fail')
318 skip_known_failure = knownfailureif(True,'This test is known to fail')
318
319
319 # A null 'decorator', useful to make more readable code that needs to pick
320 # A null 'decorator', useful to make more readable code that needs to pick
320 # between different decorators based on OS or other conditions
321 # between different decorators based on OS or other conditions
321 null_deco = lambda f: f
322 null_deco = lambda f: f
322
323
323 # Some tests only run where we can use unicode paths. Note that we can't just
324 # Some tests only run where we can use unicode paths. Note that we can't just
324 # check os.path.supports_unicode_filenames, which is always False on Linux.
325 # check os.path.supports_unicode_filenames, which is always False on Linux.
325 try:
326 try:
326 tempfile.mkdtemp(u"€")
327 f = tempfile.NamedTemporaryFile(prefix=u"tmp€")
327 except UnicodeEncodeError:
328 except UnicodeEncodeError:
328 unicode_paths = False
329 unicode_paths = False
329 else:
330 else:
330 unicode_paths = True
331 unicode_paths = True
332 f.close()
331
333
332 onlyif_unicode_paths = onlyif(unicode_paths, ("This test is only applicable "
334 onlyif_unicode_paths = onlyif(unicode_paths, ("This test is only applicable "
333 "where we can use unicode in filenames."))
335 "where we can use unicode in filenames."))
General Comments 0
You need to be logged in to leave comments. Login now