# -*- coding: utf-8 -*- # pylint: disable-msg= # $Id$ # Copyright (C) 2008 KDS Software Group http://www.kds.com.ua import nose.tools def raise_exc(exc): raise exc def raises(callback, *exceptions): """Test must raise one of expected exceptions to pass. The raised exception should pass callback check. Example use:: def callback(exc): assert exc.args[0] == 'This test passes' @raises(callback, TypeError, ValueError) def test_raises_type_error(): raise TypeError("This test passes") @raises(callback, TypeError, ValueError) def test_fails_by_assert(): raise TypeError("This test fails") @raises(callback, Exception): def test_that_fails_by_passing(): pass If you want to test many assertions about exceptions in a single test, you may want to use `assert_raises` instead. """ valid = ' or '.join([e.__name__ for e in exceptions]) def decorate(func): name = func.__name__ def newfunc(*arg, **kw): try: func(*arg, **kw) except exceptions, e: callback(e) except: raise else: message = "%s() did not raise %s" % (name, valid) raise AssertionError(err_msg) newfunc = nose.tools.make_decorator(func)(newfunc) return newfunc return decorate