Sono relativamente nuovo a Python e voglio usare un test assertRaises
per verificare un ValidationError
, che funziona bene. Tuttavia, ho molti ValidationError
e voglio assicurarmi che venga restituito quello giusto. Ho pensato di poter passare qualcosa in assertRaises
ma non sembra possibile, quindi ho pensato di fare solo un assertTrue
e controllare il messaggio di eccezione. Tuttavia, non so come accedervi. Questo è anche un buon modo per affrontare questo problema? Grazie.Asser Django/PythonRispondi al controllo dei messaggi
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"))
Ottima risposta, grazie. :) – Teekin