Python Glossary Русский

all Python 3unittestterm

unittest.TestResult

running tests

The object a runner fills with the outcome of a run: how many tests ran, which failed, which raised errors, which were skipped.

Syntax

result = unittest.TestResult()
suite.run(result)
result.wasSuccessful()

failures and errors are separate lists: failures holds failed checks (AssertionError), errors holds any other exception, such as a typo in a name or ZeroDivisionError. The items are (test, traceback text) pairs with the traceback as a string, not exception objects. wasSuccessful() is also False on an unexpected success of an @expectedFailure test, so looking at failures alone is not enough.

Examples

import unittest
class Tests(unittest.TestCase):
    def test_fail(self):
        self.assertEqual(1, 2)
    def test_error(self):
        1 / 0
    def test_ok(self):
        pass
result = unittest.TestResult()
unittest.TestLoader().loadTestsFromTestCase(Tests).run(result)
print(result.testsRun, len(result.failures), len(result.errors))  # → 3 1 1
print(result.errors[0][0].id())  # → __main__.Tests.test_error
print(result.wasSuccessful())  # → False

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy