Fixtures not loaded during testing
django, fixtures, unit-testing
Solution
Found the solution in another thread, answer from John Mee
# Import the TestCase from django.test:
# Bad: import unittest
# Bad: import django.utils.unittest
# Good: import django.test
from django.test import TestCase
class test_something(TestCase):
fixtures = ['one.json', 'two.json']
...
Doing this I got a proper error message, saying that foreign key is violated and I had to also include the fixtures for the app "auth". I exported the needed data with this command:
manage.py dumpdata auth.User auth.Group > usersandgroups.json
Using Unittest I got only the message that loading of fixture data failed, which was not very helpful.
Finally my working test looks like this:
from django.test import TestCase
class NodeTableTestCase2(TestCase):
fixtures = ['auth/auth_usersandgroups_fixture.json','core/core_fixture.json']
def setUp(self):
# Test definitions as before.
print "welcome in setup: while..nothing to setup.."
def testFixture2(self):
"""Check if initial data can be loaded correctly"""
self.assertEqual(Node.objects.all().count(), 11)
Problem
I wrote a unit test checking whether initial data is loaded correctly. However the `Node.objects.all().count()` always returns 0, thus it seems as the fixtures are not loaded at all. There is no output/error msg in the command line that fixtures are not loaded. ``` from core.models import Node class NodeTableTestCase(unittest.TestCase): fixtures = ['core/core_fixture.json'] def setUp(self): print "nothing to prepare..." def testFixture(self): """Check if initial data can be loaded correctly""" self.assertEqual(Node.objects.all().count(), 14) ``` the fixture `core_fixture.json` contains 14 nodes and I'm using this fixture as a initial data load into the db using the following command: ``` python manage.py loaddata core/core_fixture.json ``` They are located in the folder I provided in the `settings.py` setting `FIXTURE_DIRS`.