我想测试是否引发了异常,我该怎么做?
在我的models.py中,我有这个功能,我想测试的那个:
def validate_percent(value):
if not (value >= 0 and value <= 100):
raise ValidationError('error')
在我的tests.py中我试过这个:
def test_validate_percent(self):
self.assertRaises(ValidationError, validate_percent(1000))
测试的输出是:
..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']