Python Glossary Русский

all Python 3unittestterm

unittest

module

The built-in xUnit-style unit testing framework: test case classes, assert* checks, fixtures, mocks.

Syntax

import unittest

unittest ships with every Python installation, so it fits where installing packages is not allowed; tests are test_* methods of a TestCase subclass. pytest runs the same tests and lets you write them more briefly with a plain assert, so new projects often choose it. A common mistake is naming a method without the test prefix: it silently never runs; mocking dependencies lives in the unittest.mock submodule.

Examples

import unittest
class TestAdd(unittest.TestCase):
    def test_add(self):
        self.assertEqual(1 + 1, 2)
suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestAdd)
result = unittest.TextTestRunner(verbosity=0).run(suite)
print(result.testsRun, result.wasSuccessful())  # → 1 True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy