haki
haki

Reputation: 9759

Fixture inheritance in django TestCase

I have a very simple scenario

from django.test import TestCase

class BaseTest(TestCase):
    fixtures = ('users.json',)
    ...


class SpecificTest(BaseTest):
    fixtures = ('transactions.json',)
    ...

transactions has an FK to users and when SpecificTest attempts to load the fixtures I get an IntegrityError

IntegrityError: Problem installing fixtures: 
The row in table 'app_transactions' with primary key '1' has an 
invalid foreign key: app_transactions.user_id contains a value '30'   
that does not have a corresponding value in app_user.id.

This error means that the users.json fixture loaded in BaseTest was not loaded before the transactions.json fixtures (as you might expect). My question is, what is the proper way to load fixtures when subclassing tests ?

Django 1.7

Upvotes: 3

Views: 1079

Answers (1)

Alasdair
Alasdair

Reputation: 308769

When you override fixtures in a subclass, it replaces the fixtures, it doesn't extend them.

You can either explicitly repeat the fixtures:

class SpecificTest(BaseTest):
    fixtures = ('users.json', 'transactions.json',)

or refer to BaseTest.fixtures in your subclass:

class SpecificTest(BaseTest):
    fixtures = BaseTest.fixtures + ('transactions.json',)

Upvotes: 4

Related Questions