I've written Django tests using django.test.TestCase, and I'd like to use a fixture with all of my current database data to run the tests. However, if I create the fixture as follows:
python manage.py dumpdata --indent=3 > myapp/fixtures/test_data.json
when I then run the tests using python manage.py test myapp
, I get the following error:
Problem installing fixture...(traceback)
IntegrityError: Could not load auth.Permission(pk=42): duplicate key value violates unique constraint "auth_permission_content_type_id_codename_key"
DETAIL: Key (content_type_id, codename)=(14, add_record) already exists.
I read somewhere on SO that this might be caused by a pk conflict so I then tried re-creating the fixture with:
python manage.py dumpdata --natural --indent=3 > myapp/fixtures/test_data.json
But now running the test gives me:
Problem installing fixture...(traceback)
DeserializationError: 'NoneType' object has no attribute '_meta'
I've also tried variously excluding (using the --exclude
option) auth.permission
and contenttypes
(or both simultaneously), but then I get complaints about missing permissions (Key (permission_id)=(44) is not present in table "auth_permission".
) or missing content types (DeserializationError: ContentType matching query does not exist.
)
The thing is, I need permissions anyway because my tests are partly to verify that only users with specific permissions can access certain views.
I don't understand why this is happening, to be honest--my impression was that the test runner starts with a completely clean database and loads EVERYTHING from my fixture, but reading posts like this one: Django unit-testing with loading fixtures for several dependent applications problems makes it seem like maybe that's not the case.
How can I get around this? I'd much rather not have to write stuff like
User.objects.create_user(..
tons of times under def setUp(self):
in my tests just to have enough objects for them to run properly...