CKTIS / Python
Run tests and inspect the result of `TextTestRunner.run`
Run a test suite or a single test case and use the returned `TestResult` to build a custom summary without parsing the runner’s text report.
Version: Python 3.12
Syntax
runner = unittest.TextTestRunner(stream=report_stream)
result = runner.run(test)
result.testsRun
len(result.failures)
len(result.errors)
result.wasSuccessful()In Python 3.12, unittest.TextTestRunner.run takes a TestSuite or TestCase instance, runs the supplied tests, and returns a TestResult object (TextTestResult by default). Separately, the runner writes a text report to the destination set by stream, which defaults to sys.stderr.
result.testsRun counts tests that started, not tests that passed or were scheduled. result.failures and result.errors are lists of (test, formatted traceback) pairs. A failed assertion such as self.assertEqual(...) goes into failures; an unexpected exception such as a ValueError escaping a test method goes into errors. Use len(...) to count the entries in each list.
result.wasSuccessful() returns True if no failures, errors, or unexpected successes were recorded. An unexpected success is a passing test marked with @unittest.expectedFailure. Skips and expected failures do not by themselves make the method return False. Do not replace this method with a check of just the two lists or treat True as proof that any tests ran.
Examples
import io
import unittest
class ArithmeticTests(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("intentional test error")
if __name__ == "__main__":
suite = unittest.TestSuite([
ArithmeticTests("test_pass"),
ArithmeticTests("test_failure"),
ArithmeticTests("test_error"),
])
report_stream = io.StringIO()
result = unittest.TextTestRunner(stream=report_stream).run(suite)
print(f"Tests run: {result.testsRun}")
print(f"Failures: {len(result.failures)}")
print(f"Errors: {len(result.errors)}")
print(f"Successful: {result.wasSuccessful()}")Result
Tests run: 3 Failures: 1 Errors: 1 Successful: False
One test passes, one fails an assertion, and one raises an error. `io.StringIO()` captures the runner’s usual report, including elapsed time, so the console shows only the custom four-line summary.
Related lessons
- Python · Lesson 37 · Advanced · Version: 5
- Python · Lesson 34 · Intermediate · Version: 3