<p>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?</p>
<p>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.</p>
<p>For example, suppose you had a class Foo, and were testing a method bar, using data from a list called testdata:</p>
<pre><code>class TestBar(unittest.TestCase):
    def runTest(self):
        for t1, t2 in testdata:
            f = Foo(t1)
            self.assertEqual(f.bar(t2), 2)
</code></pre>
<p>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.</p>

You can use simple print statements, or any other way of writing to standard output.  You can also invoke the Python debugger anywhere in your tests.
If you use
nose
to run your tests (which I recommend), it will collect the standard output for each test and only show it to you if the test failed, so you don't have to live with the cluttered output when the tests pass.
nose
also has switches to automatically show variables mentioned in asserts, or to invoke the debugger on failed tests.  For example,
```
-s
```
(
```
--nocapture
```
) prevents the capture of standard output.