vendredi 21 février 2020

Integrity Error in running django tests with multiple instances of TestCase class

The tests defined for the first TestCase execute successfully. However, for the next TestCase I get an integrity error indicating that the fixtures from the previous TestCase were not cleaned up after the test run. This happens only with Postgres. The same tests run successfully with SQLite. If I put these tests within the same class, they run successfully even with postgres. Any help in figuring out whats going on here will be appreciated.

test_run.py :

User = get_user_model()
client = Client()

class DemoTest(TestCase):
    fixtures = ['app1fixture', 'auth', 'app2fixture', 'app3fixture']

    def test_demo(self):
        u = User.objects.create(username='Apple')
        self.assertEqual(u.username, 'Apple')

class DemoTest2(TestCase):
    fixtures = ['app1fixture', 'auth', 'app2fixture', 'app3fixture']

    def test_demo2(self):
        u = User.objects.create(username='Banana')
        self.assertEqual(u.username, 'Banana')

Error :

django.db.utils.IntegrityError: Problem installing fixture '/home/shaily/myapp/app1/fixtures/app1fixture.json': Could not load app1.Profile(pk=1): duplicate key value violates unique constraint "app1_profile_user_id_key" DETAIL: Key (user_id)=(1) already exists.

Aucun commentaire:

Enregistrer un commentaire