1 """Tools not exempt from being descended into in tracebacks"""
6 __all__ = ['make_decorator', 'raises', 'set_trace', 'timed', 'with_setup',
7 'TimeExpired', 'istest', 'nottest']
10 class TimeExpired(AssertionError):
14 def make_decorator(func):
16 Wraps a test decorator so as to properly replicate metadata
17 of the decorated function, including nose's additional stuff
18 (namely, setup and teardown).
20 def decorate(newfunc):
21 if hasattr(func, 'compat_func_name'):
22 name = func.compat_func_name
25 newfunc.__dict__ = func.__dict__
26 newfunc.__doc__ = func.__doc__
27 newfunc.__module__ = func.__module__
28 if not hasattr(newfunc, 'compat_co_firstlineno'):
29 newfunc.compat_co_firstlineno = func.func_code.co_firstlineno
31 newfunc.__name__ = name
33 # can't set func name in 2.3
34 newfunc.compat_func_name = name
39 def raises(*exceptions):
40 """Test must raise one of expected exceptions to pass.
44 @raises(TypeError, ValueError)
45 def test_raises_type_error():
46 raise TypeError("This test passes")
49 def test_that_fails_by_passing():
52 If you want to test many assertions about exceptions in a single test,
53 you may want to use `assert_raises` instead.
55 valid = ' or '.join([e.__name__ for e in exceptions])
58 def newfunc(*arg, **kw):
66 message = "%s() did not raise %s" % (name, valid)
67 raise AssertionError(message)
68 newfunc = make_decorator(func)(newfunc)
74 """Call pdb.set_trace in the calling frame, first restoring
75 sys.stdout to the real output stream. Note that sys.stdout is NOT
76 reset to whatever it was before the call once pdb is done!
81 sys.stdout = sys.__stdout__
82 pdb.Pdb().set_trace(sys._getframe().f_back)
86 """Test must finish within specified time limit to pass.
91 def test_that_fails():
95 def newfunc(*arg, **kw):
99 if end - start > limit:
100 raise TimeExpired("Time limit (%s) exceeded" % limit)
101 newfunc = make_decorator(func)(newfunc)
106 def with_setup(setup=None, teardown=None):
107 """Decorator to add setup and/or teardown methods to a test function::
109 @with_setup(setup, teardown)
110 def test_something():
113 Note that `with_setup` is useful *only* for test functions, not for test
114 methods or inside of TestCase subclasses.
116 def decorate(func, setup=setup, teardown=teardown):
118 if hasattr(func, 'setup'):
127 if hasattr(func, 'teardown'):
128 _old_t = func.teardown
134 func.teardown = teardown
140 """Decorator to mark a function or method as a test
147 """Decorator to mark a function or method as *not* a test
149 func.__test__ = False