Django Model.objects.create()数据库异常未过滤

时间:2018-06-28 22:11:30

标签: python django exception-handling pytest psycopg2

我正试图通过创建py.test单元测试来在Django中测试约束验证,方法是创建一个不存在父对象的子对象。

@pytest.mark.django_db
def test_child_with_missing_parent():
    with pytest.raises(django.db.utils.IntegrityError):
        Child.objects.create(parent_id=1337)

引发了异常,但无法捕获-异常仅在stderr中显示。我现在正在使用pytest.mark.xfail,但这实际上只是一个“跳过”-结果是1 xfailed, 1 xpassed。在这样的预期情况下如何捕捉这样的错误?

这是出现在控制台中但无法捕获的异常/错误:

self = <django.db.backends.utils.CursorWrapper object at 0x7fe1e2dea5f8>, sql = 'SET CONSTRAINTS ALL IMMEDIATE', params = None
ignored_wrapper_args = (False, {'connection': <django.db.backends.postgresql.base.DatabaseWrapper object at 0x7fe1ec724128>, 'cursor': <django.db.backends.utils.CursorWrapper object at 0x7fe1e2dea5f8>})

    def _execute(self, sql, params, *ignored_wrapper_args):
        self.db.validate_no_broken_transaction()
        with self.db.wrap_database_errors:
            if params is None:
>               return self.cursor.execute(sql)
E               psycopg2.IntegrityError: insert or update on table "myapp_child" violates foreign key constraint "myapp_re_parent_id_537da634_fk_myapp_calc"
E               DETAIL:  Key (parent_id)=(1337) is not present in table "myapp_parent".

/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py:83: IntegrityError

The above exception was the direct cause of the following exception:

self = <django.test.testcases.TestCase testMethod=__init__>

1 个答案:

答案 0 :(得分:2)

看起来您正在尝试捕获与引发的异常不同的异常。 psycopg2.IntegrityErrordjango.db.utils.IntegrityError不同-出于某种原因,Django并未在自己的包装器中包装此异常。

这应该有效:

with pytest.raises(psycopg2.IntegrityError):
    Child.objects.create(parent_id=1337)