是否可以在django的测试中使用其他应用程序中的灯具?

Tho*_*mas 9 testing django fixtures

我有2个应用程序,成员和资源.资源取决于成员.是否可以在我的资源应用测试中使用成员应用中的测试夹具?

Tho*_*mas 5

显然是的,任何设备都可以从任何应用程序加载,就像它在同一个应用程序中一样,所以要小心你的设备名称。:/


小智 5

例如,如果您有两个应用程序,一个名为“ 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
Run Code Online (Sandbox Code Playgroud)

这是一个假想的场景,您想为“ 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
Run Code Online (Sandbox Code Playgroud)

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