How do I dynamically create Python unit tests in a readable way? I have Django unit tests that are pretty much the following format:

class Tests(unittest.TestCase):
    def check(self, i, j):
        self.assertNotEquals(0, i-j)

for i in xrange(1, 4):
    for j in xrange(2, 6):
        def ch(i, j):
            return lambda self: self.check(i, j)
        setattr(Tests, "test_%r_%r" % (i, j), ch(i, j))


A function that returns a lambda that is bound as a method via setattr, which is an eyesore and as unreadable as you can get in Python without really trying to obfuscate.

How can I achieve the same functionality in a more readable way, preferably without lambda?

For reference, see the original SO question about the subject.
Don't bother with generating tests:

class Tests(unittest.TestCase):
    def check(self, i, j):
        self.assertNotEquals(0, i-j)

    def test_thingies(self):
        for i in xrange(1, 4):
            for j in xrange(2, 6):
                self.check(i,j)


It would be nicer to generate individual tests, but do you really get that much benefit out of it?

Or write a reusable function:

data = [(i,j) for i in xrange(1,4) for j in xrange(2,6)]
add_data_tests(TestClass, data, 'check')