CKTIS / Python
unittest.TestResult.failures — assertion failure list
unittest.TestResult.failures stores assertion failures as pairs containing a test object and formatted traceback text.
Version: Python 3.14; repository image, production runtime not attested
Syntax
result.failures
for test, traceback_text in result.failures:
...unittest.TestResult.failures is an instance attribute, not a method. It contains a list of (test, traceback_text) pairs: a TestCase object and a formatted traceback string. Inspect it on the result object after running tests.
When classifying an exception from an ordinary test method as a failure or an error, unittest uses the test case’s failureException type. Instances of that type or its subclasses go into failures; other exceptions go into errors. The default type is AssertionError, which a failed assertEqual() call raises by default. By contrast, an AssertionError raised in setUpClass goes into errors. Expected failures are stored separately in expectedFailures. An empty failures list therefore does not prove that the entire suite succeeded.
The example deliberately produces one failure. It checks the entry structure without printing the traceback, whose paths and line numbers depend on the file.
Examples
import unittest
class SampleTest(unittest.TestCase):
def test_total(self):
self.assertEqual(2 + 2, 5)
result = unittest.TestResult()
SampleTest("test_total").run(result)
test, traceback_text = result.failures[0]
print("test:" if False else "failures:", len(result.failures))
print("test:", test.id())
print("traceback is text:", isinstance(traceback_text, str))
print("errors:", len(result.errors))Result
failures: 1 test: __main__.SampleTest.test_total traceback is text: True errors: 0
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.