python unittests with multiple setups?

fixtures, python, sockets, unit-testing

Solution

you could do something like this:

class TestCommon(unittest.TestCase):
    def method_one(self):
        # code for your first test
        pass

    def method_two(self):
        # code for your second test
        pass

class TestWithSetupA(TestCommon):
    def SetUp(self):
        # setup for context A
        do_setup_a_stuff()

    def test_method_one(self):
        self.method_one()

    def test_method_two(self):
        self.method_two()

class TestWithSetupB(TestCommon):
    def SetUp(self):
        # setup for context B
        do_setup_b_stuff()

    def test_method_one(self):
        self.method_one()

    def test_method_two(self):
        self.method_two()

Problem

I'm working on a module using sockets with hundreds of test cases. Which is nice. Except now I need to test all of the cases with and without socket.setdefaulttimeout( 60 )... Please don't tell me cut and paste all the tests and set/remove a default timeout in setup/teardown. Honestly, I get that having each test case laid out on it's own is good practice, but i also don't like to repeat myself. This is really just testing in a different context not different tests. i see that unittest supports module level setup/teardown fixtures, but it isn't obvious to me how to convert my one test module into testing itself twice with two different setups. any help would be much appreciated.

Original source