I've solved this problem in my projects just by putting common setup code in a base class, and then putting test cases in derived classes. My derived class setUp and tearDown methods just call the super class implementations, and only do (de)initialization specific to those test cases. Also, keep in mind that you can put multiple tests in each test case, which might make sense if all the setup is the same.
class MyBaseTestCase(unittest.TestCase):
def setUp(self):
self.patcher_stdout = mock.patch('sys.stdout', StringIO())
self.patcher_stdout.start()
# Do **nothing**
def tearDown(self):
self.patcher_stdout.stop()
class StdoutOutputTestCase(MyBaseTestCase):
"""Tests whether the stuff is printed correctly."""
def setUp(self):
super(StdoutOutputTestCase, self).setUp()
# StdoutOutputTestCase specific set up code
# Do testing
def tearDown(self):
super(StdoutOutputTestCase, self).tearDown()
# StdoutOutputTestCase specific tear down code
class UserInteractionTestCase(MyBaseTestCase):
# Same pattern as StdoutOutputTestCase