我已经定义了一个自定义错误,但如果我测试是否引发了自定义错误,则会失败.
我的models.py:
class CustomError(Exception):
"""
This exception is my custom error
"""
class Company(models.Model):
name = models.CharField(max_length=200)
def test_error(self):
raise CustomError('hello')
Run Code Online (Sandbox Code Playgroud)
在我的tests.py中:
import unittest
from myapp.models import Company,Customer,Employee,Location,Product,ProductCategory,AllreadyPayedError,CustomError
class CompanyTestCase(unittest.TestCase):
def setUp(self):
self.company = Company.objects.create(name="lizto")
def test2(self):
self.assertRaises(CustomError, self.company.test_error)
Run Code Online (Sandbox Code Playgroud)
此输出的测试失败:
======================================================================
ERROR: test2 (myapp.api.tests.CompanyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/......./tests.py", line 27, in test2
self.assertRaises(CustomError, self.company.test_error)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/unittest.py", line 320, in failUnlessRaises
callableObj(*args, **kwargs)
File " /Users/....../models.py", line 17, in test_error
raise CustomError('hello')
CustomError: hello
----------------------------------------------------------------------
Ran …
Run Code Online (Sandbox Code Playgroud)