概述
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']
def test_validate_percent(self): with self.assertRaises(ValidationError): validate_percent(1000)
或与可呼叫:
def test_validate_percent(self): self.assertRaises(ValidationError,validate_percent,1000)
> http://docs.python.org/2/library/unittest.html#unittest.TestCase.assertRaises
如果您也喜欢它,动动您的小指点个赞吧