【发布时间】:2019-05-16 08:01:19
【问题描述】:
我正在定义一个 pytest 夹具来覆盖 the django_db_setup fixture。
为了安全起见,我所做的更改设置了额外的拆卸,因为有使用此固定装置的集成测试可能会产生进程,并且有时需要进行清理以防止所有东西损坏。
这似乎是合理的,并且在 pytest 文档中也有建议。但是,我不想复制粘贴与django_db_setup 完全相同的逻辑,因为我对已经存在的内容感到满意。但是,将其作为函数运行会引发弃用警告:
/usr/local/lib/python3.6/dist-packages/_pytest/fixtures.py:799:
RemovedInPytest4Warning: Fixture "django_db_setup" called directly.
Fixtures are not meant to be called directly, are created automatically
when test functions request them as parameters. See
https://docs.pytest.org/en/latest/fixture.html for more information.
在 pytest 4 中处理这种情况的推荐方法是什么?我们是否鼓励我们从我们想要覆盖的固定装置中复制粘贴代码,或者是否有另一种方法来“继承”固定装置,并注入例如自定义行为在调用它之前和之后? p>
【问题讨论】:
标签: python pytest fixtures pytest-django