class Test1(TestCase):
fixtures = ['fixture.json']
@classmethod
def tearDownClass(cls):
super(TestCase, cls).tearDownClass()
def test_demo(self):
u = User.objects.get(username='testuser1')
#some testing - removed actual tests as we can replicate the issue with even this code
class Test2(TestCase):
fixtures = ['fixture.json']
def test_demo2(self):
u = User.objects.get(username='testuser1')
#Some testing
I have these two test cases and my fixture has 2 rows in total, one for the User table with pk=1, and the other for profile table (one-to-one relation with User table)
If I run the test cases independently, both of the work perfect;y. But if I run them both at the same time, I get this error
Could not load app1.Profile(pk=1): duplicate key value violates unique constraint "app1_profile_user_id_key"
From my understanding of the documentation, Django will rollback all fixtures after the TestCase is done so that the DB is back to the point where only migrations are done and no fixture data is present.
Few StackOverflow answers suggested that we need to make an explicit call to teardown after the test case is done. So I added this code
@classmethod
def tearDownClass(cls):
super(TestCase, cls).tearDownClass()This helps in running both the test cases together. But this leads to a new issue of DB not being flushed between test cases. Filed a bug report in detail on
Bug tracker.
Is there something I'm missing here? Any help would be greatly appreciated.