在 django 单元测试中加载固定装置

发布于 2024-08-25 19:37:47 字数 432 浏览 11 评论 0原文

我正在尝试开始为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

篱下浅笙歌 2024-09-01 19:37:47

我已经在 TestCase 中指定了相对于项目根目录的路径,如下所示:

from django.test import TestCase

class MyTestCase(TestCase):
    fixtures = ['/myapp/fixtures/dump.json',]
    ...

并且它在不使用 FIXTURE_DIRS 的情况下工作

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

彡翼 2024-09-01 19:37:47

好的做法是在 settings.py 中使用 PROJECT_ROOT 变量:

import os.path
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
FIXTURE_DIRS = (os.path.join(PROJECT_ROOT, 'fixtures'),)

Good practice is using PROJECT_ROOT variable in your settings.py:

import os.path
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
FIXTURE_DIRS = (os.path.join(PROJECT_ROOT, 'fixtures'),)
假装爱人 2024-09-01 19:37:47

您的硬盘上真的有文件夹 /fixtures/ 吗?

您可能打算使用:

FIXTURE_DIRS = ('/path/to/proj_folder/fixtures/',)

Do you really have a folder /fixtures/ on your hard disk?

You probably intended to use:

FIXTURE_DIRS = ('/path/to/proj_folder/fixtures/',)
初相遇 2024-09-01 19:37:47

处理此问题的一种更好、更简洁的方法是将所有固定装置放在项目级别的一个文件夹中并加载它们,而不是创建固定装置文件夹并将固定装置放置在其中(在每个应用程序中)。

from django.core.management import call_command

class TestMachin(TestCase):

    def setUp(self):
        # Load fixtures
        call_command('loaddata', 'fixtures/myfixture', verbosity=0)

调用 call_command 相当于运行:

 manage.py loaddata /path/to/fixtures 

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.

from django.core.management import call_command

class TestMachin(TestCase):

    def setUp(self):
        # Load fixtures
        call_command('loaddata', 'fixtures/myfixture', verbosity=0)

Invoking call_command is equivalent to running :

 manage.py loaddata /path/to/fixtures 
铁憨憨 2024-09-01 19:37:47

假设您有一个名为 hello_django 的项目,其中包含 api 应用程序。

以下是为其创建固定装置的步骤:

  1. 可选步骤:从数据库创建固定装置文件:python manage.py dumpdata --format=json > api/fixtures/testdata.json
  2. 创建测试目录: api/tests
  3. api/tests 中创建空文件 __init__.py
  4. 创建测试文件:test_fixtures.py
from django.test import TestCase

class FixturesTestCase(TestCase):
  fixtures = ['api/api/fixtures/testdata.json']
  def test_it(self):
    # implement your test here
  1. 运行测试以将灯具加载到数据库中:python manage.py test api.tests

Saying you have a project named hello_django with api app.

Following are steps to create fixtures for it:

  1. Optional step: create fixture file from database: python manage.py dumpdata --format=json > api/fixtures/testdata.json
  2. Create test directory: api/tests
  3. Create empty file __init__.py in api/tests
  4. Create test file: test_fixtures.py
from django.test import TestCase

class FixturesTestCase(TestCase):
  fixtures = ['api/api/fixtures/testdata.json']
  def test_it(self):
    # implement your test here
  1. Run the test to load fixtures into the database: python manage.py test api.tests
暖风昔人 2024-09-01 19:37:47

我这样做了,我不必提供路径引用,夹具文件名对我来说就足够了。

class SomeTest(TestCase):

    fixtures = ('myfixture.json',)

I did this and I didn't have to give a path reference, the fixture file name was enough for me.

class SomeTest(TestCase):

    fixtures = ('myfixture.json',)
苍风燃霜 2024-09-01 19:37:47

您有两种选择,具体取决于您是否有固定装置,或者是否有一组用于填充数据的 Python 代码。

对于固定装置,请使用cls.fixtures,如此问题的答案中所示,

class MyTestCase(django.test.TestCase):
    fixtures = ['/myapp/fixtures/dump.json',]

对于Python ,使用cls.setUpTestData

class MyTestCase(django.test.TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.create_fixture()  # create_fixture is a custom function

setUpTestDataTestCase.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,

class MyTestCase(django.test.TestCase):
    fixtures = ['/myapp/fixtures/dump.json',]

For Python, use cls.setUpTestData:

class MyTestCase(django.test.TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.create_fixture()  # create_fixture is a custom function

setUpTestData is called by the TestCase.setUpClass.

You can use both, in which case fixtures is loaded first because setUpTestData is called after loading the fixtures.

弱骨蛰伏 2024-09-01 19:37:47

您需要导入 from django.test import TestCase 而不是 from unittest import TestCase。这解决了我的问题。

You need to import from django.test import TestCase and NOT from unittest import TestCase. That fixed the problem for me.

爺獨霸怡葒院 2024-09-01 19:37:47

如果您重写了 setUpClass 方法,请确保在该方法的第一行调用 super().setUpClass() 方法。加载装置的代码位于 TestCase 类中。

If you have overridden setUpClass method, make sure you call super().setUpClass() method as the first line in the method. The code to load fixtures is in TestCase class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文