CKTIS / Python
unittest: Assertion failures and test errors
Run a test suite and use `testsRun`, `failures`, and `errors` to build a deterministic summary without parsing the runner’s text report.
Version: Python 3.12
Syntax
runner = unittest.TextTestRunner(stream=stream)
result = runner.run(suite)
result.testsRun
result.failures
result.errors
len(result.failures)
len(result.errors)In Python 3.12, unittest.TextTestRunner.run(suite) executes the suite and returns a unittest.TestResult object (TextTestResult by default). Read the returned object rather than parsing the text report. result.testsRun counts tests that started, not tests that passed or the size of the suite before execution.
A failed self.assertEqual(...) raises the test case’s failureException, which defaults to AssertionError. When unittest classifies an exception as a failure or an error, it records it in result.failures if its type is failureException or a subclass, and in result.errors otherwise. An unhandled ValueError in a test method is therefore an error by default, but a failure if it matches the configured failureException. If the assertion fails in the body of a test method decorated with @unittest.expectedFailure and teardown and cleanup succeed, the outcome is recorded in result.expectedFailures instead. Each entry in result.failures or result.errors pairs a test object with a formatted traceback string. Use len(result.failures) and len(result.errors) to count entries.
Do not subtract both list lengths from testsRun to count passes: skips are recorded separately, and one test can produce several entries, for example with subTest. Use result.wasSuccessful() for the overall verdict: it also returns False when a test marked as an expected failure unexpectedly passes.
Examples
import io
import unittest
class DemoTests(unittest.TestCase):
def test_pass(self):
self.assertEqual(2 + 2, 4)
def test_failure(self):
self.assertEqual(2 + 2, 5)
def test_error(self):
raise ValueError("unexpected value")
suite = unittest.defaultTestLoader.loadTestsFromTestCase(DemoTests)
result = unittest.TextTestRunner(stream=io.StringIO()).run(suite)
print(f"testsRun={result.testsRun} failures={len(result.failures)} errors={len(result.errors)}")Result
testsRun=3 failures=1 errors=1
One test passes, one fails an assertion, and one raises an error. `io.StringIO()` captures the runner’s report, so the console shows only the line of counts.
Related lessons
- Python · Lesson 37 · Advanced · Version: 5