<p>Can someone explain this result to me. The first test succeeds but the second fails, although the variable tested is changed in the first test.</p>

<pre><code>&gt;&gt;&gt; class MyTest(unittest.TestCase):
    def setUp(self):
        self.i = 1
    def testA(self):
        self.i = 3
        self.assertEqual(self.i, 3)
    def testB(self):
        self.assertEqual(self.i, 3)


&gt;&gt;&gt; unittest.main()
.F
======================================================================
FAIL: testB (__main__.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "&lt;pyshell#61&gt;", line 8, in testB
AssertionError: 1 != 3

----------------------------------------------------------------------
Ran 2 tests in 0.016s
</code></pre>

[{'answer_id': 72498, 'author': 'mmaibaum', 'author_id': 12213, 'author_profile': 'https://Stackoverflow.com/users/12213', 'pm_score': 0, 'selected': False, 'text': '<p>If I recall correctly in that test framework the setUp method is run before each test</p>\n'}, {'answer_id': 72504, 'author': 'pjz', 'author_id': 8002, 'author_profile': 'https://Stackoverflow.com/users/8002', 'pm_score': 4, 'selected': False, 'text': '<p>From <a href="http://docs.python.org/lib/minimal-example.html" rel="noreferrer">http://docs.python.org/lib/minimal-example.html</a> :</p>\n\n<blockquote>\n  <p>When a setUp() method is defined, the\n  test runner will run that method prior\n  to each test.</p>\n</blockquote>\n\n<p>So setUp() gets run before both testA and testB, setting i to 1 each time.  Behind the scenes, the entire test object is actually being re-instantiated for each test, with setUp() being run on each new instantiation before the test is executed.</p>\n'}, {'answer_id': 72702, 'author': 'Jon Homan', 'author_id': 7589, 'author_profile': 'https://Stackoverflow.com/users/7589', 'pm_score': -1, 'selected': False, 'text': '<p>The setUp method, as everyone else has said, runs before every test method you write. So, when testB runs, the value of i is 1, not 3.</p>\n\n<p>You can also use a tearDown method which runs after every test method. However if one of your tests crashes, your tearDown method will never run.</p>\n'}, {'answer_id': 73791, 'author': 'Sebastian Rittau', 'author_id': 7779, 'author_profile': 'https://Stackoverflow.com/users/7779', 'pm_score': 4, 'selected': True, 'text': '<p>Each test is run using a new instance of the MyTest class. That means if you change self in one test, changes will not carry over to other tests, since self will refer to a different instance.</p>\n\n<p>Additionally, as others have pointed out, setUp is called before each test.</p>\n'}, {'answer_id': 215576, 'author': 'Roman Plášil', 'author_id': 16590, 'author_profile': 'https://Stackoverflow.com/users/16590', 'pm_score': 0, 'selected': False, 'text': '<p>From a methodological point of view, individual tests should be independent, otherwise it can produce more hard-to-find bugs. Imagine for instance that testA and testB would be called in a different order.</p>\n'}]
['https://Stackoverflow.com/questions/72422', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9510/']