On this page
unittest.FunctionTestCase
Prev: unittest.expectedFailure | Up: unittest | Next: unittest.installHandler
Rel: unittest.addModuleCleanup, unittest.doModuleCleanups, unittest.enterModuleContext, unittest.expectedFailure
Ref: docs.python.org — unittest.FunctionTestCase
Tags: #public #python3 #stdlib #unittest
Import
import unittest
from unittest import FunctionTestCase
Signature & Overview
unittest.FunctionTestCase(testFunc, setUp=None, tearDown=None, description=None)
A test case that wraps a test function.
Methods & Attributes
FunctionTestCase.addClassCleanup(function, /, *args, **kwargs)— Same as addCleanup, except the cleanup items are called even if setUpClass fails (unlike tearDownClass).FunctionTestCase.addCleanup(self, function, /, *args, **kwargs)— Add a function, with arguments, to be called when the test is completed. Functions added are called on a LIFO basis and are called after tearDown on test failure or success.FunctionTestCase.addTypeEqualityFunc(self, typeobj, function)— Add a type specific assertEqual style function to compare a type.FunctionTestCase.assertAlmostEqual(self, first, second, places=None, msg=None, delta=None)— Fail if the two objects are unequal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that theFunctionTestCase.assertCountEqual(self, first, second, msg=None)— Asserts that two iterables have the same elements, the same number of times, without regard to order.FunctionTestCase.assertDictEqual(self, d1, d2, msg=None)—FunctionTestCase.assertDictEqualmethod/property.FunctionTestCase.assertEndsWith(self, s, suffix, msg=None)—FunctionTestCase.assertEndsWithmethod/property.FunctionTestCase.assertEqual(self, first, second, msg=None)— Fail if the two objects are unequal as determined by the '==' operator.FunctionTestCase.assertFalse(self, expr, msg=None)— Check that the expression is false.FunctionTestCase.assertGreater(self, a, b, msg=None)— Just like self.assertTrue(a > b), but with a nicer default message.FunctionTestCase.assertGreaterEqual(self, a, b, msg=None)— Just like self.assertTrue(a >= b), but with a nicer default message.FunctionTestCase.assertHasAttr(self, obj, name, msg=None)—FunctionTestCase.assertHasAttrmethod/property.FunctionTestCase.assertIn(self, member, container, msg=None)— Just like self.assertTrue(a in b), but with a nicer default message.FunctionTestCase.assertIs(self, expr1, expr2, msg=None)— Just like self.assertTrue(a is b), but with a nicer default message.FunctionTestCase.assertIsInstance(self, obj, cls, msg=None)— Same as self.assertTrue(isinstance(obj, cls)), with a nicer default message.FunctionTestCase.assertIsNone(self, obj, msg=None)— Same as self.assertTrue(obj is None), with a nicer default message.FunctionTestCase.assertIsNot(self, expr1, expr2, msg=None)— Just like self.assertTrue(a is not b), but with a nicer default message.FunctionTestCase.assertIsNotNone(self, obj, msg=None)— Included for symmetry with assertIsNone.FunctionTestCase.assertIsSubclass(self, cls, superclass, msg=None)—FunctionTestCase.assertIsSubclassmethod/property.FunctionTestCase.assertLess(self, a, b, msg=None)— Just like self.assertTrue(a < b), but with a nicer default message.FunctionTestCase.assertLessEqual(self, a, b, msg=None)— Just like self.assertTrue(a <= b), but with a nicer default message.FunctionTestCase.assertListEqual(self, list1, list2, msg=None)— A list-specific equality assertion.FunctionTestCase.assertLogs(self, logger=None, level=None)— Fail unless a log message of level level or higher is emitted on logger_name or its children. If omitted, level defaults to INFO and logger defaults to the root logger.FunctionTestCase.assertMultiLineEqual(self, first, second, msg=None)— Assert that two multi-line strings are equal.FunctionTestCase.assertNoLogs(self, logger=None, level=None)— Fail unless no log messages of level level or higher are emitted on logger_name or its children.FunctionTestCase.assertNotAlmostEqual(self, first, second, places=None, msg=None, delta=None)— Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that theFunctionTestCase.assertNotEndsWith(self, s, suffix, msg=None)—FunctionTestCase.assertNotEndsWithmethod/property.FunctionTestCase.assertNotEqual(self, first, second, msg=None)— Fail if the two objects are equal as determined by the '!=' operator.FunctionTestCase.assertNotHasAttr(self, obj, name, msg=None)—FunctionTestCase.assertNotHasAttrmethod/property.FunctionTestCase.assertNotIn(self, member, container, msg=None)— Just like self.assertTrue(a not in b), but with a nicer default message.FunctionTestCase.assertNotIsInstance(self, obj, cls, msg=None)— Included for symmetry with assertIsInstance.FunctionTestCase.assertNotIsSubclass(self, cls, superclass, msg=None)—FunctionTestCase.assertNotIsSubclassmethod/property.FunctionTestCase.assertNotRegex(self, text, unexpected_regex, msg=None)— Fail the test if the text matches the regular expression.FunctionTestCase.assertNotStartsWith(self, s, prefix, msg=None)—FunctionTestCase.assertNotStartsWithmethod/property.FunctionTestCase.assertRaises(self, expected_exception, *args, **kwargs)— Fail unless an exception of class expected_exception is raised by the callable when invoked with specified positional and keyword arguments. If a different type of exception isFunctionTestCase.assertRaisesRegex(self, expected_exception, expected_regex, *args, **kwargs)— Asserts that the message in a raised exception matches a regex.FunctionTestCase.assertRegex(self, text, expected_regex, msg=None)— Fail the test unless the text matches the regular expression.FunctionTestCase.assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None)— An equality assertion for ordered sequences (like lists and tuples).FunctionTestCase.assertSetEqual(self, set1, set2, msg=None)— A set-specific equality assertion.FunctionTestCase.assertStartsWith(self, s, prefix, msg=None)—FunctionTestCase.assertStartsWithmethod/property.
Usage
import unittest
def check_math() -> None:
assert 2 + 2 == 4
case = unittest.FunctionTestCase(check_math)
result = unittest.TestResult()
case.run(result)
assert result.wasSuccessful()
Gotchas
- Adapter for legacy plain test functions: For new tests, subclass
unittest.TestCasedirectly (or usepytest) so you get richassertEqualdiffs and fixtures.
REPL Inspect
from unittest import FunctionTestCase
help(FunctionTestCase)
Linked from
Related notes
- tuts/python3/stdlib_module/unittest/addModuleCleanup Linked here; links to this page; shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/doModuleCleanups Linked here; links to this page; shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/enterModuleContext Linked here; links to this page; shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/expectedFailure Linked here; links to this page; shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/installHandler Linked here; links to this page; shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/unittest/IsolatedAsyncioTestCase shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/main shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/registerResult shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/removeHandler shared tags: #stdlib, #unittest