在django单元测试中加载装置

时间:2022-11-19 10:13:11

I'm trying to start writing unit tests for django and I'm having some questions about fixtures:

我试着开始为django编写单元测试,我有一些关于设备的问题:

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.

我为我的整个项目db(不是特定的应用程序)做了一个fixture,我想在每次测试中加载它,因为看起来只加载某些应用程序的fixture是不够的。

I'd like to have the fixture stored in /proj_folder/fixtures/proj_fixture.json.

我想把夹具存储在/proj_folder/fixture /proj_fixture.json中。

I've set the FIXTURE_DIRS = ('/fixtures/',) in my settings.py. Then in my testcase I'm trying

我在settings.py中设置了FIXTURE_DIRS = ('/fixtures/')。在我的测试中,我尝试。

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!

但是我的装置装不下。如何解决这个问题?如何添加搜索装置的位置?通常,是否可以为每个应用程序中的每个测试加载整个test_db的fixture(如果非常小的话)?谢谢!

7 个解决方案

#1


28  

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

你的硬盘上真的有文件夹/固定装置吗?

You probably intended to use:

您可能打算使用:

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

#2


76  

I've specified path relative to project root in the TestCase like so:

我在TestCase中指定了与project root相关的路径,如下所示:

from django.test import TestCase

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

and it worked without using FIXTURE_DIRS

它不用FIXTURE_DIRS就能工作

#3


29  

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

好的做法是在设置中使用PROJECT_ROOT变量。

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

#4


6  

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.

与其创建fixures文件夹并将fixture放在其中(在每个应用程序中),更好、更整洁的处理方法是将所有fixture放在项目级别的一个文件夹中并装载它们。

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 :

调用call_command等同于运行:

 manage.py loaddata /path/to/fixtures 

#5


2  

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

我做了这个,我不需要给出路径引用,这个fixture文件名对我来说已经足够了。

class SomeTest(TestCase):

    fixtures = ('myfixture.json',)

#6


1  

You have two options, depending on whether you have a fixture, or you have a set of Python code to populate the data.

您有两个选项,取决于您是否有一个fixture,或者您是否有一组Python代码来填充数据。

For fixtures, use cls.fixtures, like shown in an answer to this question,

对于设备,使用cls。附着物,如这个问题的答案所示,

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

For Python, use cls.setUpTestData:

对于Python,使用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.

setUpTestData被TestCase.setUpClass调用。

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

您可以使用这两种方法,在这种情况下,首先加载fixture,因为在加载fixture之后会调用setUpTestData。

#7


1  

Let's say your project name is hello_django, it has an app named api. Following is steps to create fixtures in api:

假设你的项目名为hello_django,它有一个名为api的应用程序。以下是在api中创建fixture的步骤:

  1. Optional step: create fixture file from database: python manage.py dumpdata --format=json > api/fixtures/testdata.json
  2. 可选步骤:从数据库:python管理中创建fixture文件。py dumpdata—format=json > api/fixtures/testdata.json
  3. Create test directory: api/tests
  4. 创建测试目录:api /测试
  5. Create empty file __init__.py in api/tests
  6. __init__创建空文件。py api /测试
  7. Create test file: test_fixtures.py
  8. 创建测试文件:test_fixtures.py

在django单元测试中加载装置

  1. Run the test (will load your fixtures in database): python manage.py test api.tests
  2. 运行测试(将在数据库中加载您的fixture): python管理。py测试api.tests

#1


28  

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

你的硬盘上真的有文件夹/固定装置吗?

You probably intended to use:

您可能打算使用:

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

#2


76  

I've specified path relative to project root in the TestCase like so:

我在TestCase中指定了与project root相关的路径,如下所示:

from django.test import TestCase

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

and it worked without using FIXTURE_DIRS

它不用FIXTURE_DIRS就能工作

#3


29  

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

好的做法是在设置中使用PROJECT_ROOT变量。

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

#4


6  

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.

与其创建fixures文件夹并将fixture放在其中(在每个应用程序中),更好、更整洁的处理方法是将所有fixture放在项目级别的一个文件夹中并装载它们。

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 :

调用call_command等同于运行:

 manage.py loaddata /path/to/fixtures 

#5


2  

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

我做了这个,我不需要给出路径引用,这个fixture文件名对我来说已经足够了。

class SomeTest(TestCase):

    fixtures = ('myfixture.json',)

#6


1  

You have two options, depending on whether you have a fixture, or you have a set of Python code to populate the data.

您有两个选项,取决于您是否有一个fixture,或者您是否有一组Python代码来填充数据。

For fixtures, use cls.fixtures, like shown in an answer to this question,

对于设备,使用cls。附着物,如这个问题的答案所示,

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

For Python, use cls.setUpTestData:

对于Python,使用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.

setUpTestData被TestCase.setUpClass调用。

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

您可以使用这两种方法,在这种情况下,首先加载fixture,因为在加载fixture之后会调用setUpTestData。

#7


1  

Let's say your project name is hello_django, it has an app named api. Following is steps to create fixtures in api:

假设你的项目名为hello_django,它有一个名为api的应用程序。以下是在api中创建fixture的步骤:

  1. Optional step: create fixture file from database: python manage.py dumpdata --format=json > api/fixtures/testdata.json
  2. 可选步骤:从数据库:python管理中创建fixture文件。py dumpdata—format=json > api/fixtures/testdata.json
  3. Create test directory: api/tests
  4. 创建测试目录:api /测试
  5. Create empty file __init__.py in api/tests
  6. __init__创建空文件。py api /测试
  7. Create test file: test_fixtures.py
  8. 创建测试文件:test_fixtures.py

在django单元测试中加载装置

  1. Run the test (will load your fixtures in database): python manage.py test api.tests
  2. 运行测试(将在数据库中加载您的fixture): python管理。py测试api.tests