在 django 单元测试中加载固定装置
我正在尝试开始为 django 编写单元测试,并且对固定装置有一些疑问:
我为整个项目数据库(不是某些应用程序)制作了固定装置,并且我想为每个测试加载它,因为它看起来像加载仅某些应用程序的固定装置是不够的。
我希望将夹具存储在 /proj_folder/fixtures/proj_fixture.json
中。
我已在 settings.py 中设置了 FIXTURE_DIRS = ('/fixtures/',)
。 然后在我的测试用例中我正在尝试,
fixtures = ['proj_fixture.json']
但我的装置没有加载。 如何解决这个问题? 如何添加搜索灯具的地方? 一般来说,是否可以为每个应用程序中的每个测试加载整个 test_db 的固定装置(如果它很小)? 谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我已经在 TestCase 中指定了相对于项目根目录的路径,如下所示:
并且它在不使用
FIXTURE_DIRS
的情况下工作I've specified path relative to project root in the TestCase like so:
and it worked without using
FIXTURE_DIRS
好的做法是在 settings.py 中使用 PROJECT_ROOT 变量:
Good practice is using PROJECT_ROOT variable in your settings.py:
您的硬盘上真的有文件夹
/fixtures/
吗?您可能打算使用:
Do you really have a folder
/fixtures/
on your hard disk?You probably intended to use:
处理此问题的一种更好、更简洁的方法是将所有固定装置放在项目级别的一个文件夹中并加载它们,而不是创建固定装置文件夹并将固定装置放置在其中(在每个应用程序中)。
调用
call_command
相当于运行:Instead of creating fixures folder and placing fixtures in them (in every app), a better and neater way to handle this would be to put all fixtures in one folder at the project level and load them.
Invoking
call_command
is equivalent to running :假设您有一个名为
hello_django
的项目,其中包含api
应用程序。以下是为其创建固定装置的步骤:
python manage.py dumpdata --format=json > api/fixtures/testdata.json
api/tests
api/tests
中创建空文件__init__.py
python manage.py test api.tests
Saying you have a project named
hello_django
withapi
app.Following are steps to create fixtures for it:
python manage.py dumpdata --format=json > api/fixtures/testdata.json
api/tests
__init__.py
inapi/tests
python manage.py test api.tests
我这样做了,我不必提供路径引用,夹具文件名对我来说就足够了。
I did this and I didn't have to give a path reference, the fixture file name was enough for me.
您有两种选择,具体取决于您是否有固定装置,或者是否有一组用于填充数据的 Python 代码。
对于固定装置,请使用
cls.fixtures
,如此问题的答案中所示,对于Python ,使用
cls.setUpTestData
:setUpTestData
由TestCase.setUpClass
调用。您可以同时使用两者,在这种情况下,首先加载装置,因为在加载装置后调用
setUpTestData
。You have two options, depending on whether you have a fixture, or you have a set of Python code to populate the data.
For fixtures, use
cls.fixtures
, like shown in an answer to this question,For Python, use
cls.setUpTestData
:setUpTestData
is called by theTestCase.setUpClass
.You can use both, in which case fixtures is loaded first because
setUpTestData
is called after loading the fixtures.您需要导入
from django.test import TestCase
而不是from unittest import TestCase
。这解决了我的问题。You need to import
from django.test import TestCase
and NOTfrom unittest import TestCase
. That fixed the problem for me.如果您重写了
setUpClass
方法,请确保在该方法的第一行调用super().setUpClass()
方法。加载装置的代码位于 TestCase 类中。If you have overridden
setUpClass
method, make sure you callsuper().setUpClass()
method as the first line in the method. The code to load fixtures is in TestCase class.