On this page
unittest.TestCase
Prev: unittest.skipUnless | Up: unittest | Next: unittest.TestLoader
Rel: unittest.addModuleCleanup, unittest.doModuleCleanups, unittest.enterModuleContext, unittest.expectedFailure
Ref: docs.python.org — unittest.TestCase
Tags: #public #python3 #stdlib #unittest
Import
import unittest
from unittest import TestCase
Signature & Overview
unittest.TestCase(methodName='runTest')
A class whose instances are single test cases.
Methods & Attributes
TestCase.addClassCleanup(function, /, *args, **kwargs)— Same as addCleanup, except the cleanup items are called even if setUpClass fails (unlike tearDownClass).TestCase.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.TestCase.addTypeEqualityFunc(self, typeobj, function)— Add a type specific assertEqual style function to compare a type.TestCase.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 theTestCase.assertCountEqual(self, first, second, msg=None)— Asserts that two iterables have the same elements, the same number of times, without regard to order.TestCase.assertDictEqual(self, d1, d2, msg=None)—TestCase.assertDictEqualmethod/property.TestCase.assertEndsWith(self, s, suffix, msg=None)—TestCase.assertEndsWithmethod/property.TestCase.assertEqual(self, first, second, msg=None)— Fail if the two objects are unequal as determined by the '==' operator.TestCase.assertFalse(self, expr, msg=None)— Check that the expression is false.TestCase.assertGreater(self, a, b, msg=None)— Just like self.assertTrue(a > b), but with a nicer default message.TestCase.assertGreaterEqual(self, a, b, msg=None)— Just like self.assertTrue(a >= b), but with a nicer default message.TestCase.assertHasAttr(self, obj, name, msg=None)—TestCase.assertHasAttrmethod/property.TestCase.assertIn(self, member, container, msg=None)— Just like self.assertTrue(a in b), but with a nicer default message.TestCase.assertIs(self, expr1, expr2, msg=None)— Just like self.assertTrue(a is b), but with a nicer default message.TestCase.assertIsInstance(self, obj, cls, msg=None)— Same as self.assertTrue(isinstance(obj, cls)), with a nicer default message.TestCase.assertIsNone(self, obj, msg=None)— Same as self.assertTrue(obj is None), with a nicer default message.TestCase.assertIsNot(self, expr1, expr2, msg=None)— Just like self.assertTrue(a is not b), but with a nicer default message.TestCase.assertIsNotNone(self, obj, msg=None)— Included for symmetry with assertIsNone.TestCase.assertIsSubclass(self, cls, superclass, msg=None)—TestCase.assertIsSubclassmethod/property.TestCase.assertLess(self, a, b, msg=None)— Just like self.assertTrue(a < b), but with a nicer default message.TestCase.assertLessEqual(self, a, b, msg=None)— Just like self.assertTrue(a <= b), but with a nicer default message.TestCase.assertListEqual(self, list1, list2, msg=None)— A list-specific equality assertion.TestCase.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.TestCase.assertMultiLineEqual(self, first, second, msg=None)— Assert that two multi-line strings are equal.TestCase.assertNoLogs(self, logger=None, level=None)— Fail unless no log messages of level level or higher are emitted on logger_name or its children.TestCase.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 theTestCase.assertNotEndsWith(self, s, suffix, msg=None)—TestCase.assertNotEndsWithmethod/property.TestCase.assertNotEqual(self, first, second, msg=None)— Fail if the two objects are equal as determined by the '!=' operator.TestCase.assertNotHasAttr(self, obj, name, msg=None)—TestCase.assertNotHasAttrmethod/property.TestCase.assertNotIn(self, member, container, msg=None)— Just like self.assertTrue(a not in b), but with a nicer default message.TestCase.assertNotIsInstance(self, obj, cls, msg=None)— Included for symmetry with assertIsInstance.TestCase.assertNotIsSubclass(self, cls, superclass, msg=None)—TestCase.assertNotIsSubclassmethod/property.TestCase.assertNotRegex(self, text, unexpected_regex, msg=None)— Fail the test if the text matches the regular expression.TestCase.assertNotStartsWith(self, s, prefix, msg=None)—TestCase.assertNotStartsWithmethod/property.TestCase.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 isTestCase.assertRaisesRegex(self, expected_exception, expected_regex, *args, **kwargs)— Asserts that the message in a raised exception matches a regex.TestCase.assertRegex(self, text, expected_regex, msg=None)— Fail the test unless the text matches the regular expression.TestCase.assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None)— An equality assertion for ordered sequences (like lists and tuples).TestCase.assertSetEqual(self, set1, set2, msg=None)— A set-specific equality assertion.TestCase.assertStartsWith(self, s, prefix, msg=None)—TestCase.assertStartsWithmethod/property.
Usage
import unittest
class MathTests(unittest.TestCase):
def test_add(self) -> None:
self.assertEqual(2 + 3, 5)
def test_zero_div(self) -> None:
with self.assertRaises(ZeroDivisionError):
_ = 1 / 0
suite = unittest.defaultTestLoader.loadTestsFromTestCase(MathTests)
res = unittest.TestResult()
suite.run(res)
assert res.wasSuccessful() and res.testsRun == 2
Gotchas
- Test methods MUST start with
test_: Other methods are ignored by discovery. - Use
self.subTest(i=i)in loops: Lets all parameter cases run and report individually instead of stopping on the first failure.
REPL Inspect
from unittest import TestCase
help(TestCase)
Linked from
Related notes
- tuts/python3/stdlib_module/unittest/skipUnless Linked here; links to this page; shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/TestLoader 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/addModuleCleanup Linked here; shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/doModuleCleanups Linked here; shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/enterModuleContext Linked here; shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/expectedFailure Linked here; shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/FunctionTestCase shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/installHandler shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/IsolatedAsyncioTestCase shared tags: #stdlib, #unittest