If I run the following test case:
class DummyTestCase(TestCase):
@classmethod
def setUpClass(cls):
cls.foo = 42
super().setUpClass()
def test_1(self):
self.foo += 1
print(f"=================> {self.foo}")
def test_2(self):
self.foo += 1
print(f"=================> {self.foo}")
I get the following result:
tests/test_dummy.py::DummyTestCase::test_1 =================> 43
PASSED
tests/test_dummy.py::DummyTestCase::test_2 =================> 43
PASSED
How comes that I don't get 44
for the second test?
I understand that this would not be desirable for test isolation, but that would be what I would have expected if the test case behaved like a common class on which self.foo += 1
is called twice. What implementation details in unittest make it behave like that?