0

Why when creating instance as a class attribute:

class ModelTestsProducts(TestCase):
    # Create example objects in the database

    product_category = models.Product_category.objects.create(
            name='Spring'
        )
    product_segment = models.Product_segment.objects.create(
            name='PS1',
            product_category=self.product_category
        )
    product_group = models.Product_group.objects.create(
            name='PG1',
            product_segment=self.product_segment
        )
    def test_product_category(self):
        self.assertEqual(str(self.product_category), self.product_category.name)
    def test_product_segment(self):
        self.assertEqual(str(self.product_segment), self.product_segment.name)
    def test_product_group(self):
        self.assertEqual(str(self.product_group), self.product_group.name)

I am getting following error when running test for the 2nd time?

django.db.utils.IntegrityError: duplicate key value violates unique constraint "products_product_category_name_key"
DETAIL:  Key (name)=(dadsad) already exists.

When I use setUp method and then create objects insite this setUp method it works fine, but I cant understand why the above method somehow creates every object multiple times and thus fails the unique constraint set in the model.
Is it because django test suite somehow calls this class everytime every test function is run, thus every attribute is assigned multiple times?

But then if I move the object assignment outside the class (in the test file) then I also get this duplicate error, so that would mean whole test file is being called multiple times every time test is being run.

One more thing I use docker to run this Django app and and run django test from docker-compose command.