Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

TestResult: a four-line unittest report

Run a test suite and use the returned `TestResult` to report how many tests started, how many failures and errors were recorded, and whether the run was successful.

Version: Python 3.12

Syntax

result = unittest.TextTestRunner(stream=stream).run(suite)
result.testsRun
len(result.failures)
len(result.errors)
result.wasSuccessful()

In Python 3.12, unittest.TextTestRunner.run(suite) executes the suite and, by default, returns a TextTestResult, a subclass of TestResult. The scope TestResult.testsRun,failures,errors,wasSuccessful covers three attributes and one method on that result. Keep the returned object to build a custom report instead of parsing the runner's text.

result.testsRun is an integer count of tests started, not tests passed or tests scheduled. A skip still counts if the test's start was recorded. A class setup error, on the other hand, can be recorded without any of that class's tests starting.

result.failures and result.errors are lists, not counters. The first records failures such as a failed assertEqual; the second records errors such as an unexpected ValueError. Each entry pairs an object identifying the test with a formatted traceback string. Use len(...) to count entries.

result.wasSuccessful() is a method, so include the parentheses. It returns True only if there are no recorded failures, errors, or unexpected successes (tests marked with @unittest.expectedFailure that passed). Skips and expected failures do not by themselves make it return False.

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


if __name__ == "__main__":
    suite = unittest.defaultTestLoader.loadTestsFromTestCase(DemoTests)
    with io.StringIO() as stream:
        result = unittest.TextTestRunner(stream=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 `ValueError`. The runner writes its usual report to `io.StringIO`, so only the four custom lines appear in the console.

Related lessons

  • Python · Lesson 34 · Intermediate · Version: 3

Sources