gle*_*ich 37 django unit-testing fixtures
我正在尝试开始为django编写单元测试,我对夹具有一些疑问:
我制作了我的整个项目数据库(不是某些应用程序)的夹具,我想为每个测试加载它,因为看起来只加载某个应用程序的夹具是不够的.
我想将夹具存储在/proj_folder/fixtures/proj_fixture.json中.
我已经FIXTURE_DIRS = ('/fixtures/',)
在我的settings.py中设置了.然后在我的测试用例中我正在尝试
fixtures = ['proj_fixture.json']
Run Code Online (Sandbox Code Playgroud)
但是我的装置没有加载.怎么解决这个问题?如何添加搜索灯具的地方?一般情况下,为每个应用程序中的每个测试加载整个test_db的夹具是否可以(如果它非常小)?谢谢!
Evg*_*eny 87
我在TestCase中指定了相对于项目根目录的路径,如下所示:
from django.test import TestCase
class MyTestCase(TestCase):
fixtures = ['/myapp/fixtures/dump.json',]
...
Run Code Online (Sandbox Code Playgroud)
它没有使用它 FIXTURE_DIRS
Ben*_*mes 30
你真的/fixtures/
在硬盘上有一个文件夹吗?
您可能打算使用:
FIXTURE_DIRS = ('/path/to/proj_folder/fixtures/',)
Run Code Online (Sandbox Code Playgroud)
Leo*_*kov 30
好的做法是在settings.py中使用PROJECT_ROOT变量:
import os.path
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
FIXTURE_DIRS = (os.path.join(PROJECT_ROOT, 'fixtures'),)
Run Code Online (Sandbox Code Playgroud)
Arm*_*nce 13
不是创建fixures文件夹并在其中放置灯具(在每个应用程序中),处理这个的更好和更简洁的方法是将所有灯具放在项目级别的一个文件夹中并加载它们.
from django.core.management import call_command
class TestMachin(TestCase):
def setUp(self):
# Load fixtures
call_command('loaddata', 'fixtures/myfixture', verbosity=0)
Run Code Online (Sandbox Code Playgroud)
调用 call_command
等同于运行:
manage.py loaddata /path/to/fixtures
Run Code Online (Sandbox Code Playgroud)
说你有一个名为工程hello_django
与api
应用。
以下是为其创建夹具的步骤:
python manage.py dumpdata --format=json > api/fixtures/testdata.json
api/tests
__init__.py
中api/tests
python manage.py test api.tests