unittests with different parameters

Ulrich Eckhardt ulrich.eckhardt at dominolaser.com
Wed Nov 24 09:10:03 EST 2010


Short update on what I've settled for generating test functions for various
input data:

# test case with common test function
class MyTest(unittest.TestCase):
    def _test_invert_flags(self, input, flags, expected):
        res = do_invert(input, flags)
        self.assertEqual(res, expected)

# test definitions for the various invert flags
tests = [((10, 20), INVERT_NONE, (10, 20)),
         ((10, 20), INVERT_X, (-10, 20)),
         ((10, 20), INVERT_Y, (10, -20))]

# add test to the test case class
for input, flags, expected in tests:
    def test(self):
        self._test_invert_flags(input, flags, expected)
    test.__doc__ = "testing invert flags %s" % flags
    setattr(MyTest, "test_invert_flags_%s" % flags, test)


Yes, the names of the test functions would clash if I tested the same flags
twice, in the real code that doesn't happen (enumerate is my friend!).

Thanks all!

Uli

-- 
Domino Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932




More information about the Python-list mailing list