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

时间:2021-06-19 18:55:06

I have 2 apps, members and resources. resources depends on members. Is it possible to use test fixtures from the members app in my tests for the resources app?

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

2 个解决方案

#1


3  

For example, if you have two apps, one named for "App1" and the other named "App2", and the structure of your project is something like this:

例如,如果您有两个应用程序,一个名为“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

this is a imaginary scenario, and you want to write test script for "APP2" but your test script may need the data from "APP1", in other words you need the fixtures in "APP1"

这是一个想象的场景,你想为“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

as you saw, just write the fixture name of "APP1" in the fixtures list, very intelligent and easy.

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

#2


1  

Apparently yes, any fixture can be loaded from any app as if it was in the same app, so be wary of what you name your fixtures. :/

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

#1


3  

For example, if you have two apps, one named for "App1" and the other named "App2", and the structure of your project is something like this:

例如,如果您有两个应用程序,一个名为“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

this is a imaginary scenario, and you want to write test script for "APP2" but your test script may need the data from "APP1", in other words you need the fixtures in "APP1"

这是一个想象的场景,你想为“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

as you saw, just write the fixture name of "APP1" in the fixtures list, very intelligent and easy.

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

#2


1  

Apparently yes, any fixture can be loaded from any app as if it was in the same app, so be wary of what you name your fixtures. :/

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