[Python] Outputting data from unit test in Python

Discussão em 'Python' iniciado por Stack, Agosto 23, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    I am aware of the ability to create a customized message, which can carry some information, but sometimes you might deal with more complex data, that can't easily be represented as a string.

    For example, suppose you had a class Foo, and were testing a method bar, using data from a list called testdata:

    class TestBar(unittest.TestCase):
    def runTest(self):
    for t1, t2 in testdata:
    f = Foo(t1)
    self.assertEqual(f.bar(t2), 2)


    If the test failed, I might want to output t1, t2 and/or f, to see why this particular data resulted in a failure. By output, I mean that the variables can be accessed like any other variables, after the test has been run.

    Continue reading...

Compartilhe esta Página