【问题标题】:Is it possible to use fixtures from other apps in django's tests?是否可以在 django 的测试中使用来自其他应用程序的固定装置?
【发布时间】:2011-09-06 04:45:10
【问题描述】:

我有 2 个应用程序、成员和资源。资源取决于成员。是否可以在资源应用程序的测试中使用成员应用程序中的测试装置?

【问题讨论】:

    标签: django testing fixtures


    【解决方案1】:

    显然是的,任何灯具都可以从任何应用程序中加载,就好像它在同一个应用程序中一样,因此请注意您为灯具命名的名称。 :/

    【讨论】:

      【解决方案2】:

      例如,如果您有两个应用程序,一个名为“App1”,另一个名为“App2”,您的项目结构如下:

      myproject/
      ----APP1/
      --------models/
      ------------app_1_model.py
      --------tests/
      ------------test_app1.py
      --------fixtures/
      ------------fixture_app1_number_1.json
      ------------fixture_app1_number_2.json
      ----APP2/
      --------models/
      ------------app_2_model.py
      --------tests/
      ------------test_app2.py
      --------fixtures/
      ------------fixture_app2_number_1.json
      ------------fixture_app2_number_2.json
      ------------fixture_app2_number_3.json
      

      这是一个虚构的场景,你想为“APP2”编写测试脚本,但你的测试脚本可能需要“APP1”中的数据,换句话说,你需要“APP1”中的夹具

      from APP1.models.app_1_model import *
      class TestApp2(TestCase):
         fixtures = ['fixture_app2_number_1','fixture_app2_number_2','fixture_app2_number_3','fixture_app1_number_1']
         def test_function_one(self):
           pass
      

      如您所见,只需在灯具列表中写入“APP1”的灯具名称,非常智能和简单。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-27
        • 2013-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-18
        • 2013-04-15
        相关资源
        最近更新 更多