我对Python比较新,想用assertRaises
测试来检查ValidationError
,它工作正常。但是,我有很多ValidationError
,我想确保正确的返回。我想我可以通过assertRaises
的东西,但它看起来不像我可以,所以我想我会做一个assertTrue
并检查异常消息。但是,我不知道如何访问它。这是解决这个问题的好方法吗?谢谢。Django/Python assertRaises with message check
class DailyEntriesTests(TestCase):
def test_cant_have_ip_and_user(self):
u = createUser(False)
de = createDailyEntry(u, "1.1.1.1", 1)
with self.assertRaises(ValidationError) as cm:
de.full_clean()
# this line bombs - message doesn't exist. I also tried "error_code" like I saw in the documentation, but that doesn't work
print(cm.exception.message)
self.assertTrue(cm.exception.message.contains("Both"))
很好的答案,谢谢。 :) – Teekin 2018-01-01 23:44:43