我有以下测试方法
def test_fingerprintBadFormat(self):
"""
A C{BadFingerPrintFormat} error is raised when unsupported
formats are requested.
"""
with self.assertRaises(keys.BadFingerPrintFormat) as em:
keys.Key(self.rsaObj).fingerprint('sha256-base')
self.assertEqual('Unsupported fingerprint format: sha256-base',
em.exception.message)
这是异常类。在
^{pr2}$这个测试方法在Python2中可以很好地工作,但是在python3中失败,并显示以下消息
builtins.AttributeError: 'BadFingerPrintFormat' object has no attribute 'message'
如何测试Python3中的错误消息。我不喜欢使用asserRaisesRegex
来测试正则表达式,而不是异常消息。在
在python3中,
.message
属性已从异常中删除。使用.args[0]
代替:或者使用
^{pr2}$str(em.exception)
获得相同的值:这对Python 2和Python 3都有效:
相关问题 更多 >
编程相关推荐