mardi 18 septembre 2018

Django test data persisting across multiple tests despite being TestCase

I have a a test case that uses setUp and looks like this:

from django.test import TestCase, Client, TransactionTestCase
...
class TestPancakeView(TestCase):

    def setUp(self):
        self.client = Client()

        # The Year objects are empty (none here - expected)
        print(Year.objects.all())

        # When I run these Test by itself, these are empty (expected)
        # When I run the whole file, this prints out a number of
        # Pancake objects causing these tests to fail - these objects appear
        # to be persisting from previous tests
        print(Pancake.objects.all())

        # This sets up my mock/test data
        data_setup.basic_setup(self)

        # This now prints out the expected years that data_setup creates
        print(Year.objects.all())

        # This prints all the objects that I should have 
        # But if the whole file is ran - it's printing objects
        # That were created in other tests
        print(Pancake.objects.all())

        [...tests and such...]

data_setup.py is a file that just creates all the appropriate test-data for my tests when it needs them. I'm using Factory Boy for creating test data.

When I run TestPancakeView by itself - my tests pass as expected.

When I run my entire test_views.py file, my TestPancakeView tests fail.

If I change it to TransactionTestCase - it still fails when I run the whole file.

I'm creating the Pancake test data like this:

        f.PancakeFactory.create_batch(
            2,
            flavor=blueberry,
            intensity='high'
        )

No other tests exhibit this behavior, they all act the same way when I run them individually or when I run them as part of the entire file.

Aucun commentaire:

Enregistrer un commentaire