Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Module unittest

Organize test methods in TestCase classes and execute them as suites.

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

Syntax

unittest.TestSuite([case]).run(result)

Organize test methods in TestCase classes and execute them as suites.

A TestResult records successes implicitly, assertion failures separately from unexpected test errors, and other outcomes such as skips. Each standard test method has its own case instance.

The example constructs three cases and reads the result without relying on runner time or formatting.

Examples

import unittest
class Checks(unittest.TestCase):
    def test_ok(self):
        self.assertEqual(2 + 2, 4)
    def test_fail(self):
        self.assertEqual(1, 2)
    def test_error(self):
        raise ValueError("input")
suite = unittest.defaultTestLoader.loadTestsFromTestCase(Checks)
result = unittest.TestResult()
suite.run(result)
print(result.testsRun, len(result.failures), len(result.errors), result.wasSuccessful())

Result

3 1 1 False

The example constructs three cases and reads the result without relying on runner time or formatting.

Sources