【问题标题】:PyTest-fixtures: Let data persist (Django)PyTest-fixtures:让数据持久化(Django)
【发布时间】: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


    【解决方案1】:

    我会将您想要在普通 Web 应用程序中使用的固定装置部分提取到某种类型的工厂模块或类中。然后针对新部件编写测试以确认工厂按预期工作。

    但我不会尝试直接在 Web 应用程序中使用 pytest 固定装置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多