Outputting data from unit test in Python

前端 未结 14 2443
孤城傲影
孤城傲影 2020-12-02 05:45

If I\'m writing unit tests in python (using the unittest module), is it possible to output data from a failed test, so I can examine it to help deduce what caused the error?

相关标签:
14条回答
  • 2020-12-02 06:25

    Expanding @F.C. 's answer, this works quite well for me:

    class MyTest(unittest.TestCase):
        def messenger(self, message):
            try:
                self.assertEqual(1, 2, msg=message)
            except AssertionError as e:      
                print "\nMESSENGER OUTPUT: %s" % str(e),
    
    0 讨论(0)
  • 2020-12-02 06:27

    How about catching the exception that gets generated from the assertion failure? In your catch block you could output the data however you wanted to wherever. Then when you were done you could re-throw the exception. The test runner probably wouldn't know the difference.

    Disclaimer: I haven't tried this with python's unit test framework but have with other unit test frameworks.

    0 讨论(0)
提交回复
热议问题