python - 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__>
解决方案
看起来您正试图捕获与引发的异常不同的异常。psycopg2.IntegrityError
与 -不一样django.db.utils.IntegrityError
,出于某种原因,Django 没有将这个异常包装在自己的包装器中。
这应该有效:
with pytest.raises(psycopg2.IntegrityError):
Child.objects.create(parent_id=1337)
推荐阅读
- python - 重构一个有很多库的python项目,这些库有“from smthng import *”
- python - 在 Google Dataflow 上运行管道时,Apache Beam 的 FileBasedSource 出现令人困惑的错误,超过 GCS 上的约 240 万个文件
- oauth-2.0 - Azure 多租户应用程序 - 请求管理员同意自己应用程序的角色
- java - 当我们从后台服务调用意图时,意图在 android 10 中不起作用
- sapui5 - 如何为 SAPUI5 库提供缺少的翻译文件?
- javascript - Javascript 从动态表创建图表
- javascript - 赛普拉斯:匹配通过变量传递的子字符串的不区分大小写方式
- google-apps-script - Google TypeError:ScriptApp.newTrigger(...).forSpreadsheet(...).timeBased 不是函数
- python - 从具有特定条件的列中的字典中删除所有非数字符号
- javascript - TypeError: Object(...) is not a function with react-select-search (npm package)