How to get crash options in Python unittest?

I am running an assertEqual test case for a list of methods in a specific class. These methods expand from a string form into something called with getattr() .

How can I get unittest to tell me the method that failed? Meaning: how can I get unittest to print to output certain parameters that caused the statement to fail?

Any advice was greatly appreciated.

thanks

+4
source share
1 answer

You can pass assertEqual the third argument (technically the fourth if you consider yourself), which is an error message that it will return. So the following should do more or less of what you are 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) 
+5
source

Source: https://habr.com/ru/post/1302777/


All Articles