Python Glossary Русский

all Python 3unittestterm

unittest.FunctionTestCase

testing

A wrapper that turns a plain function into a unittest test case, so legacy test functions can be run by a runner without rewriting them as classes.

Syntax

unittest.FunctionTestCase(testFunc, setUp=None, tearDown=None, description=None)

The function has no self, so there is no self.assertEqual either: you are left with a bare assert, which disappears together with the check under python -O. A failing assert counts as a failure, any other exception as an error. Treat it as a bridge for migrating old code; new tests are easier to write as TestCase subclasses.

Examples

import unittest
def check_sum():
    assert sum([1, 2, 3]) == 6
def check_upper():
    assert 'a'.upper() == 'a'
suite = unittest.TestSuite([unittest.FunctionTestCase(check_sum), unittest.FunctionTestCase(check_upper)])
result = unittest.TestResult()
suite.run(result)
print(result.testsRun, len(result.failures), len(result.errors))  # → 2 1 0

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy