【发布时间】:2021-02-21 20:30:29
【问题描述】:
我喜欢 pytest-django 固定装置并在我的 django 应用程序中使用它们。
但有时我希望有数据可以玩。
到目前为止,pytest 夹具仅在临时数据库中。
我的目标:我想在我的 django-web GUI 中有一个按钮。如果我按下 按钮应该在普通数据库中调用我的 pytest 固定装置列表。
如果我尝试直接在 django 视图中调用固定装置,我会收到以下消息:
Failed: Fixture "waiting_for_activation_user" called directly. Fixtures are not meant to be called directly,
but are created automatically when test functions request them as parameters.
See https://docs.pytest.org/en/stable/fixture.html for more information about fixtures, and
https://docs.pytest.org/en/stable/deprecations.html#calling-fixtures-directly about how to update your code.
当然我可以忽略测试装置的存在,并在 Django ORM 方法update_or_create() 的帮助下实现“创建测试数据”按钮。但这并不好,因为
我需要执行两次测试数据的创建
有没有人有解决方案,所以我只需要编写一次代码来创建测试数据,我可以使用它两次(一次用于 pytext 夹具,一次用于按钮)?
【问题讨论】:
标签: django pytest pytest-django