2014-08-27 15:17:06 +00:00
|
|
|
from __future__ import unicode_literals
|
2014-08-25 22:09:38 +00:00
|
|
|
"""
|
|
|
|
Patch courtesy of:
|
|
|
|
https://marmida.com/blog/index.php/2012/08/08/monkey-patching-assert_raises/
|
|
|
|
"""
|
|
|
|
|
|
|
|
# code for monkey-patching
|
|
|
|
import nose.tools
|
2014-08-27 15:17:06 +00:00
|
|
|
|
2014-08-25 22:09:38 +00:00
|
|
|
# let's fix nose.tools.assert_raises (which is really unittest.assertRaises)
|
|
|
|
# so that it always supports context management
|
2014-08-27 15:17:06 +00:00
|
|
|
|
2014-08-25 22:09:38 +00:00
|
|
|
# in order for these changes to be available to other modules, you'll need
|
|
|
|
# to guarantee this module is imported by your fixture before either nose or
|
|
|
|
# unittest are imported
|
2014-08-27 15:17:06 +00:00
|
|
|
|
2014-08-25 22:09:38 +00:00
|
|
|
try:
|
|
|
|
nose.tools.assert_raises(Exception)
|
|
|
|
except TypeError:
|
|
|
|
# this version of assert_raises doesn't support the 1-arg version
|
|
|
|
class AssertRaisesContext(object):
|
|
|
|
def __init__(self, expected):
|
|
|
|
self.expected = expected
|
2014-08-27 15:17:06 +00:00
|
|
|
|
2014-08-25 22:09:38 +00:00
|
|
|
def __enter__(self):
|
|
|
|
return self
|
2014-08-27 15:17:06 +00:00
|
|
|
|
2014-08-25 22:09:38 +00:00
|
|
|
def __exit__(self, exc_type, exc_val, tb):
|
|
|
|
self.exception = exc_val
|
2017-01-19 03:46:51 +00:00
|
|
|
if issubclass(exc_type, self.expected):
|
|
|
|
return True
|
2014-08-25 22:09:38 +00:00
|
|
|
nose.tools.assert_equal(exc_type, self.expected)
|
|
|
|
# if you get to this line, the last assertion must have passed
|
|
|
|
# suppress the propagation of this exception
|
|
|
|
return True
|
2014-08-27 15:17:06 +00:00
|
|
|
|
2014-08-25 22:09:38 +00:00
|
|
|
def assert_raises_context(exc_type):
|
|
|
|
return AssertRaisesContext(exc_type)
|
2014-08-27 15:17:06 +00:00
|
|
|
|
2014-08-25 22:09:38 +00:00
|
|
|
nose.tools.assert_raises = assert_raises_context
|