Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

unittest.TestResult.wasSuccessful — checking test results

The wasSuccessful() method reports whether results recorded so far contain no failures, errors, or unexpected successes. It does not run tests itself.

Version: Python 3.14; repository image, production runtime not attested

Syntax

result.wasSuccessful()

unittest.TestResult.wasSuccessful() is an instance method called without arguments as result.wasSuccessful(). It returns a bool summarizing results recorded so far in result; it does not run tests.

The standard implementation returns False when failures, errors, or unexpectedSuccesses are nonempty. Skips (skipped) and expected failures (expectedFailures) alone do not cause False. A passing test decorated with @unittest.expectedFailure is an unexpected success and causes False.

An empty result returns True: this does not prove that any tests ran or that the entire suite finished. Also check testsRun if you require tests to have run. The example explicitly runs two tests, collecting their results in one object.

Examples

import unittest

class Checks(unittest.TestCase):
    def test_pass(self):
        self.assertEqual(2 + 2, 4)

    @unittest.expectedFailure
    def test_unexpected_success(self):
        self.assertTrue(True)

result = unittest.TestResult()
print(result.testsRun, result.wasSuccessful())
Checks('test_pass').run(result)
print(result.testsRun, result.wasSuccessful())
Checks('test_unexpected_success').run(result)
print(result.testsRun, result.wasSuccessful())

Result

0 True
1 True
2 False

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources