Outputting data from unit test in Python

前端 未结 14 2497
孤城傲影
孤城傲影 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:19

    I think I might have been overthinking this. One way I've come up with that does the job, is simply to have a global variable, that accumulates the diagnostic data.

    Somthing like this:

    log1 = dict()
    class TestBar(unittest.TestCase):
        def runTest(self):
            for t1, t2 in testdata:
                f = Foo(t1) 
                if f.bar(t2) != 2: 
                    log1("TestBar.runTest") = (f, t1, t2)
                    self.fail("f.bar(t2) != 2")
    

    Thanks for the replies. They have given me some alternative ideas for how to record information from unit tests in python.

提交回复
热议问题