Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 382
gleb.pitsevich Avatar asked Mar 18 '10 14:03

gleb.pitsevich


People also ask

How do I load all fixtures in Django?

By default, Django only loads fixtures into the default database. Use before_scenario to load the fixtures in all of the databases you have configured if your tests rely on the fixtures being loaded in all of them. You can read more about it in the Multiple database docs.

How do Django fixtures work?

fixtures . A fixture is a collection of data that Django knows how to import into a database. The most straightforward way of creating a fixture if you've already got some data is to use the manage.py dumpdata command.

What is SimpleTestCase?

If your tests make any database queries, use subclasses TransactionTestCase or TestCase . SimpleTestCase. databases. SimpleTestCase disallows database queries by default. This helps to avoid executing write queries which will affect other tests since each SimpleTestCase test isn't run in a transaction.

What is test fixture in unit test framework?

In generic xUnit, a test fixture is all the things that must be in place in order to run a test and expect a particular outcome. Frequently fixtures are created by handling setUp() and tearDown() events of the unit testing framework.


1 Answers

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

like image 87
Evgeny Avatar answered Oct 01 '22 10:10

Evgeny