Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

TestResult.testsRun: count the tests that started

Run a suite with `unittest.TextTestRunner.run()` and read `testsRun`, `failures`, and `errors` from the returned result to build a summary.

Version: Python 3.12

Syntax

result = unittest.TextTestRunner().run(suite)
result.testsRun  # int
len(result.failures)
len(result.errors)

In Python 3.12, unittest.TextTestRunner.run(suite) executes the suite and returns a result object. TestResult.testsRun is an integer attribute: once the run returns, read result.testsRun without parentheses. The standard count increases when a test case starts, not when it passes. An individually skipped case counts even though its method body does not run. Calls to subTest() do not add to the count separately.

result.failures and result.errors are lists of entries with formatted tracebacks. A failed assertion such as assertEqual() goes in failures; an unexpected exception such as RuntimeError goes in errors. Use len() to count the entries in each list. Don't subtract those counts from testsRun to calculate how many tests passed: skips are recorded separately in result.skipped, and one case can produce multiple entries, for example when several subtests fail.

To count the cases in the suite instead, call suite.countTestCases() before running it. With failfast=True, the run can stop before all cases start. An error in setUpClass() can be recorded in errors even though no case in that class started. Read the counts from the result object rather than parsing the text report.

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 RuntimeError("unexpected error")

    @unittest.skip("demonstrate a skipped test")
    def test_skip(self):
        self.fail("this body is not executed")


suite = unittest.TestSuite(
    DemoTests(name)
    for name in ("test_pass", "test_failure", "test_error", "test_skip")
)
with io.StringIO() as report:
    result = unittest.TextTestRunner(stream=report, failfast=False).run(suite)

print(f"testsRun={result.testsRun}")
print(f"failures={len(result.failures)}")
print(f"errors={len(result.errors)}")
print(f"skipped={len(result.skipped)}")

Result

testsRun=4
failures=1
errors=1
skipped=1

One case passes, one fails an assertion, one raises `RuntimeError`, and one is skipped. `io.StringIO` captures the runner's report, so the console shows only the four counts, without tracebacks or elapsed time.

Related lessons

  • Python · Lesson 37 · Advanced · Version: 5

Sources