32π
Do you really have a folder /fixtures/
on your hard disk?
You probably intended to use:
FIXTURE_DIRS = ('/path/to/proj_folder/fixtures/',)
116π
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
- [Django]-How do I convert datetime.timedelta to minutes, hours in Python?
- [Django]-Django: Converting an entire set of a Model's objects into a single dictionary
- [Django]-Best way to write an image to a Django HttpResponse()
34π
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'),)
- [Django]-How should I write tests for Forms in Django?
- [Django]-Django modifying the request object
- [Django]-Django: Where to put helper functions?
20π
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
- [Django]-Django Aggregation: Summation of Multiplication of two fields
- [Django]-Django Rest Framework: Dynamically return subset of fields
- [Django]-UUID as default value in Django model
9π
Saying you have a project named hello_django
with api
app.
Following are steps to create fixtures for it:
- Optional step: create fixture file from database:
python manage.py dumpdata --format=json > api/fixtures/testdata.json
- Create test directory:
api/tests
- Create empty file
__init__.py
inapi/tests
- 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
- Run the test to load fixtures into the database:
python manage.py test api.tests
- [Django]-How do I use pagination with Django class based generic ListViews?
- [Django]-How to get the ID of a just created record in Django?
- [Django]-What's the best way to handle Django's objects.get?
3π
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',)
- [Django]-Django β view sql query without publishing migrations
- [Django]-What is a "slug" in Django?
- [Django]-Django models: get list of id
2π
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.
- [Django]-Django: guidelines for speeding up template rendering performance
- [Django]-What's the difference between staff, admin, superuser in django?
- [Django]-Overriding AppConfig.ready()
1π
You need to import from django.test import TestCase
and NOT from unittest import TestCase
. That fixed the problem for me.
- [Django]-Is there a way to undo a migration on Django and uncheck it from the list of showmigrations?
- [Django]-Django: Staff Decorator
- [Django]-How to Unit test with different settings in Django?
0π
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.
- [Django]-How to query as GROUP BY in Django?
- [Django]-Is it possible to use FastAPI with Django?
- [Django]-Default value for user ForeignKey with Django admin