python - Test if ValidationError was raised -
i want test if exception raised how can that?
in models.py have function, 1 want test:
def validate_percent(value): if not (value >= 0 , value <= 100): raise validationerror('error')
in tests.py tried this:
def test_validate_percent(self): self.assertraises(validationerror, validate_percent(1000))
the output of test is:
..e ====================================================================== error: test_validate_percent (tm.tests.models.helpers.helperstestcase) ---------------------------------------------------------------------- traceback (most recent call last): file "/...py", line 21, in test_validate_percent self.assertraises(validationerror, validate_percent(1000)) file "/....py", line 25, in validate_percent raise validationerror(u'error' % value) validationerror: ['error']
assertraises
used context manager:
def test_validate_percent(self): self.assertraises(validationerror): validate_percent(1000)
or callable:
def test_validate_percent(self): self.assertraises(validationerror, validate_percent, 1000)