Django/Python assertRaises with message check -
i relatively new python , want utilize assertraises
test check validationerror
, works ok. however, have many validationerror
s , want create sure right 1 returned. figured pass assertraises
doesn't can, figured asserttrue
, check exception message. however, don't know how access it. way approach issue? thanks.
class dailyentriestests(testcase): def test_cant_have_ip_and_user(self): u = createuser(false) de = createdailyentry(u, "1.1.1.1", 1) self.assertraises(validationerror) cm: de.full_clean() # line bombs - message doesn't exist. tried "error_code" saw in documentation, doesn't work print(cm.exception.message) self.asserttrue(cm.exception.message.contains("both"))
you can utilize assertraisesregexp
.
with self.assertraisesregexp(validationerror, "both"): de.full_clean()
when utilize context manager 2nd argument regular look search through exception's string representation.
python django validation unit-testing
No comments:
Post a Comment