Fixtures

Rather than perform the tedious task of creating the MyCalc objects in every test, our TestCase object can have a setUp() method that automatically creates any resources our tests need.

For example, take a look at the following code:

    def setUp(self):        self.mycalc1_0 = mycalc.MyCalc(1, 0)        self.mycalc36_12 = mycalc.MyCalc(36, 12)

Now, every test case can use these objects to run its tests. The setUp() method will be rerun before every test, so these objects will always be reset between test methods. If we have items that need to cleaned up after each test, we can define a tearDown() method, which will be run after each test (in this case, it's not necessary).

Now, for example, our test_add() method can be much simpler:

 def test_add(self): ...

Get Python GUI Programming with Tkinter now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.