0

How do I persist changes made within the same object inheriting from TestCase in unitttest? I've referred to Persist variable changes between tests in unittest?. And the following codes works well.

from unittest import TestCase, main as unittest_main

    class TestSimpleFoo(TestCase):

        def setUp(self):
            pass

        def test_a(self):
            TestSimpleFoo.foo = 'can'

        def test_f(self):
            self.assertEqual(TestSimpleFoo.foo, 'can')

    if __name__ == '__main__':
        unittest_main()

However, If I change the test name "test_a" to "test_u", the code will be failed. Anyone can tell how could this happen? thanks.

Community
  • 1
  • 1
Yvonne
  • 1

2 Answers2

0

Resolved. I ignored the case execution order

Yvonne
  • 1
0

As you already noticed, the execution order is important here. But, in fact, the deeper problem is, that your tests are not independent from each other. Ideally, each test behaves like a complete program of its own, starting from a defined clean starting state, and cleaning up after execution.

Dirk Herrmann
  • 5,550
  • 1
  • 21
  • 47