我是Python的新手,我想使用assertRaises
测试来判断ValidationError
,它工作得很好.但是,我有很多ValidationError
,我想确保退回正确的.我想我可以将一些东西传递给assertRaises
,但是看起来我不能,所以我想我只需要执行assertTrue
并判断异常消息.但是,我不知道如何访问它.这是处理这个问题的好方法吗?谢谢.
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"))