Skip to content

Instantly share code, notes, and snippets.

@kjagiello
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjagiello/dd1cf153a2c17bcb3255 to your computer and use it in GitHub Desktop.
Save kjagiello/dd1cf153a2c17bcb3255 to your computer and use it in GitHub Desktop.
Making your life exceptionally easier
from functools import wraps
class NoExceptionRaisedError(Exception):
pass
def exceptional(func):
@wraps(func)
def wrapper(*args, **kwargs):
func(*args, **kwargs)
raise NoExceptionRaisedError
return wrapper
if __name__ == '__main__':
import unittest
class ReallyGoodExceptionError(Exception):
pass
@exceptional
def good_function():
raise ReallyGoodExceptionError('hello mother')
@exceptional
def bad_function():
pass
class ExceptionallyGoodTestCase(unittest.TestCase):
def test_correct_function(self):
with self.assertRaises(ReallyGoodExceptionError):
good_function()
def test_bad_function(self):
with self.assertRaises(NoExceptionRaisedError):
bad_function()
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment