ID EN
Unittest

TestCase

Python 3.11

Instances of the TestCase class represent the logical test units in the unittest universe. This class is intended to be used as a base class, with specific tests being implemented by concrete subclasses. This class implements the interface needed by the test runner to allow it to drive the tests, and methods that the test code can use to check for and report various kinds of failure.

Syntax

PYTHON
class unittest.TestCase(methodName='runTest')

Examples

Example 1
PYTHON
@classmethod
def setUpClass(cls):
    ...
Example 2
PYTHON
@classmethod
def tearDownClass(cls):
    ...
Example 3
PYTHON
with self.assertRaises(SomeException):
    do_something()
Example 4
PYTHON
with self.assertRaises(SomeException) as cm:
    do_something()

the_exception = cm.exception
self.assertEqual(the_exception.error_code, 3)
Example 5
PYTHON
self.assertRaisesRegex(ValueError, "invalid literal for.*XYZ'$",
                       int, 'XYZ')

See Also

TestCase unittest TestCase TestCase TestCase TestCase TestCase AssertionError