我有一个脚本将生成一个JSON文件(让我称之为data.json),对于我的django应用程序,我通常可以通过运行命令来测试它
python manage.py testserver data.json
但是,我希望在单元测试中运行这个东西,而不是通过shell运行(因为它会启动服务器,永远不会返回到shell)。我不需要运行任何依赖于这个夹具的测试。我只想确保生成的夹具能够被加载。
发布于 2018-12-05 10:35:36
Django自己的TestCase支持通过类级fixtures属性自动设置和拆卸固定装置。例如:
from django.test import TestCase
class MyTest(TestCase):
# Must live in <your_app>/fixtures/data.json
fixtures = ['data.json']
def test_something(self):
# When this runs, data.json will already have been loaded
...但是,由于您只想检查是否可以加载夹具,而不是将其用作测试的一部分,所以您可以在测试代码的某个地方调用loaddata命令。
例如:
from django.core.management import call_command
call_command('loaddata', '/path/to/data.json')发布于 2018-12-05 05:37:28
可以使用命令在代码中运行Django管理命令。
from django.core.management import call_command
from django.core.management.commands import testserver
call_command('testserver', 'data.json')https://stackoverflow.com/questions/53624975
复制相似问题