Loading fixtures in django unit tests
I'm trying to start writing unit tests for django and I'm having some questions about fixtures:
I made a fixture of my whole project db (not certain application) and I want to load it for each test, because it looks like loading only the fixture for certain app won't be enough.
I'd like to have the fixture stored in /proj_folder/fixtures/proj_fixture.json.
I've set the FIXTURE_DIRS = ('/fixtures/',)
in my settings.py. Then in my testcase I'm trying
fixtures = ['proj_fixture.json']
but my fixtures don't load. How can this be solved? How to add the place for searching fixtures? In general, is it ok to load the fixture for the whole test_db for each test in each app (if it's quite small)? Thanks!
Do you really have a folder /fixtures/
on your hard disk?
You probably intended to use:
FIXTURE_DIRS = ('/path/to/proj_folder/fixtures/',)
I've specified path relative to project root in the TestCase like so:
from django.test import TestCase
class MyTestCase(TestCase):
fixtures = ['/myapp/fixtures/dump.json',]
...
and it worked without using FIXTURE_DIRS
良好的做法是在settings.py中使用PROJECT_ROOT变量:
import os.path
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
FIXTURE_DIRS = (os.path.join(PROJECT_ROOT, 'fixtures'),)
链接地址: http://www.djcxy.com/p/52298.html
上一篇: 如何使用测试夹具与其他框架,然后单元测试
下一篇: 在django单元测试中加载装置