[Answered ]-Immutable variable for unit tests

2👍

When you set self.foo = FOO in the setUp method, you are not copying the dictionary, you are just assigning it to self.foo. When you set self.foo['a'], this is altering the original dictionary FOO, so it affects other tests.

You could avoid this problem by copying the dictionary in the setUp method.

class TestFoo(TestCase):
    def setUp(self):
        self.foo = FOO.copy()

If the dictionary contains other mutable values, then you might have to use deepcopy

import copy

class TestFoo(TestCase):
    def setUp(self):
        self.foo = copy.deepcopy(FOO)

Leave a comment