Auto merge of #2778 - syd0:add-python-assert, r=daira

Add assert_raises_message to the python test framework.
This commit is contained in:
Homu 2020-09-03 14:26:54 +00:00
commit 3caa784970
2 changed files with 14 additions and 5 deletions

View File

@ -48,7 +48,7 @@ log = logging.getLogger("BitcoinRPC")
class JSONRPCException(Exception):
def __init__(self, rpc_error):
Exception.__init__(self)
Exception.__init__(self, rpc_error.get("message"))
self.error = rpc_error
def EncodeDecimal(o):

View File

@ -518,12 +518,21 @@ def assert_greater_than(thing1, thing2):
raise AssertionError("%s <= %s"%(str(thing1),str(thing2)))
def assert_raises(exc, fun, *args, **kwds):
assert_raises_message(exc, None, fun, *args, **kwds)
def assert_raises_message(ExceptionType, errstr, func, *args, **kwargs):
"""
Asserts that func throws and that the exception contains 'errstr'
in its message.
"""
try:
fun(*args, **kwds)
except exc:
pass
func(*args, **kwargs)
except ExceptionType as e:
if errstr is not None and errstr not in str(e):
raise AssertionError("Invalid exception string: Couldn't find %r in %r" % (
errstr, str(e)))
except Exception as e:
raise AssertionError("Unexpected exception raised: "+type(e).__name__)
raise AssertionError("Unexpected exception raised: " + type(e).__name__)
else:
raise AssertionError("No exception raised")