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

时间:2011-09-06 04:45:11

标签: django testing fixtures

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

2 个答案:

答案 0 :(得分:4)

例如,如果你有两个应用程序,一个名为" 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"在灯具列表中,非常聪明和容易。

答案 1 :(得分:3)

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