首页 > 解决方案 > Django Model.objects.create() DB 异常没有过滤

问题描述

我正在尝试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__>

标签: pythondjangoexception-handlingpytestpsycopg2

解决方案


看起来您正试图捕获与引发的异常不同的异常。psycopg2.IntegrityError与 -不一样django.db.utils.IntegrityError,出于某种原因,Django 没有将这个异常包装在自己的包装器中。

这应该有效:

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

推荐阅读