views:

43

answers:

1

I'm running an assertEqual test case for a list of methods in a particular class. These methods are expanded from string form to something callable using getattr().

How can I get unittest to tell me the particular method which failed? Meaning: how can I get unittest to print to stdout the particular parameters which caused the failure of the assert?

Any advice greatly appreciated.

Thanks

+3  A: 

You can pass assertEqual a third argument (technically fourth if you count self), which is the error message it will return. So the following should do more or less what you're looking for:

class MethodTest(TestCase):
    def test_method(self):
        obj = MyClass()
        for method in "frob", "defrob", "refrob":
             self.assertEqual(getattr(obj, method)(), 42, "obj.%s() is not equal to 42" % method)
jcdyer
bingo! Thanks jcd.
norm