Django unittest assertRaise用于自定义错误

时间:2021-09-11 05:11:07

I have defined a custom error but if I test if custom error gets raised, it fails.

我已经定义了一个自定义错误,但如果我测试是否引发了自定义错误,则会失败。

My 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')

and in my tests.py:

在我的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)

The test fails with this output:

此输出的测试失败:

======================================================================
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 18 tests in 1.122s

Anybody an idea what I should do to test if CustomError gets raised

任何人都知道我应该怎么做来测试是否引发了CustomError

3 个解决方案

#1


You could catch the error and assert that it occured.

你可以捕获错误并断言它发生了。

eg: (untested)

def test2(self)
    error_occured = False
    try:
        self.company.test_error()
    except CustomError:
        error_occured = True

    self.assertTrue(error_ocurred)

Seems far from ideal but would unblock you.

似乎远非理想,但会阻止你。

#2


Thanks Andy for your answer the problem was however that I was using the wrong/different kinds of imports: In my settings in my INSTALLED_APPS I had myproj.myapp

感谢Andy的回答,问题是我使用了错误/不同种类的导入:在我的INSTALLED_APPS设置中我有myproj.myapp

After I changed:

我换了之后:

from myapp.models import Company,CustomError 

To:

from myproj.myapp.models import Company,CustomError 

It worked as expected

它按预期工作

#3


Assert Raises seems to be more unintuitive to use than other unit tests. I think this post gives a pretty good picture of how to use it (without just providing a work around): Why isn't assertRaises catching my Attribute Error using python unittest?

Assert Raises似乎比其他单元测试更不直观。我认为这篇文章给出了如何使用它的非常好的图片(不仅提供了解决方法):为什么assertRaises没有使用python unittest捕获我的属性错误?

Cheers!

#1


You could catch the error and assert that it occured.

你可以捕获错误并断言它发生了。

eg: (untested)

def test2(self)
    error_occured = False
    try:
        self.company.test_error()
    except CustomError:
        error_occured = True

    self.assertTrue(error_ocurred)

Seems far from ideal but would unblock you.

似乎远非理想,但会阻止你。

#2


Thanks Andy for your answer the problem was however that I was using the wrong/different kinds of imports: In my settings in my INSTALLED_APPS I had myproj.myapp

感谢Andy的回答,问题是我使用了错误/不同种类的导入:在我的INSTALLED_APPS设置中我有myproj.myapp

After I changed:

我换了之后:

from myapp.models import Company,CustomError 

To:

from myproj.myapp.models import Company,CustomError 

It worked as expected

它按预期工作

#3


Assert Raises seems to be more unintuitive to use than other unit tests. I think this post gives a pretty good picture of how to use it (without just providing a work around): Why isn't assertRaises catching my Attribute Error using python unittest?

Assert Raises似乎比其他单元测试更不直观。我认为这篇文章给出了如何使用它的非常好的图片(不仅提供了解决方法):为什么assertRaises没有使用python unittest捕获我的属性错误?

Cheers!