On this page
unittest.IsolatedAsyncioTestCase
Prev: unittest.installHandler | Up: unittest | Next: unittest.main
Rel: unittest.addModuleCleanup, unittest.doModuleCleanups, unittest.enterModuleContext, unittest.expectedFailure
Ref: docs.python.org — unittest.IsolatedAsyncioTestCase
Tags: #public #python3 #stdlib #unittest
Import
import unittest
from unittest import IsolatedAsyncioTestCase
Signature & Overview
unittest.IsolatedAsyncioTestCase(methodName='runTest')
unittest.IsolatedAsyncioTestCase class.
Methods & Attributes
IsolatedAsyncioTestCase.addAsyncCleanup(self, func, /, *args, **kwargs)—IsolatedAsyncioTestCase.addAsyncCleanupmethod/property.IsolatedAsyncioTestCase.addClassCleanup(function, /, *args, **kwargs)— Same as addCleanup, except the cleanup items are called even if setUpClass fails (unlike tearDownClass).IsolatedAsyncioTestCase.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.IsolatedAsyncioTestCase.addTypeEqualityFunc(self, typeobj, function)— Add a type specific assertEqual style function to compare a type.IsolatedAsyncioTestCase.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 theIsolatedAsyncioTestCase.assertCountEqual(self, first, second, msg=None)— Asserts that two iterables have the same elements, the same number of times, without regard to order.IsolatedAsyncioTestCase.assertDictEqual(self, d1, d2, msg=None)—IsolatedAsyncioTestCase.assertDictEqualmethod/property.IsolatedAsyncioTestCase.assertEndsWith(self, s, suffix, msg=None)—IsolatedAsyncioTestCase.assertEndsWithmethod/property.IsolatedAsyncioTestCase.assertEqual(self, first, second, msg=None)— Fail if the two objects are unequal as determined by the '==' operator.IsolatedAsyncioTestCase.assertFalse(self, expr, msg=None)— Check that the expression is false.IsolatedAsyncioTestCase.assertGreater(self, a, b, msg=None)— Just like self.assertTrue(a > b), but with a nicer default message.IsolatedAsyncioTestCase.assertGreaterEqual(self, a, b, msg=None)— Just like self.assertTrue(a >= b), but with a nicer default message.IsolatedAsyncioTestCase.assertHasAttr(self, obj, name, msg=None)—IsolatedAsyncioTestCase.assertHasAttrmethod/property.IsolatedAsyncioTestCase.assertIn(self, member, container, msg=None)— Just like self.assertTrue(a in b), but with a nicer default message.IsolatedAsyncioTestCase.assertIs(self, expr1, expr2, msg=None)— Just like self.assertTrue(a is b), but with a nicer default message.IsolatedAsyncioTestCase.assertIsInstance(self, obj, cls, msg=None)— Same as self.assertTrue(isinstance(obj, cls)), with a nicer default message.IsolatedAsyncioTestCase.assertIsNone(self, obj, msg=None)— Same as self.assertTrue(obj is None), with a nicer default message.IsolatedAsyncioTestCase.assertIsNot(self, expr1, expr2, msg=None)— Just like self.assertTrue(a is not b), but with a nicer default message.IsolatedAsyncioTestCase.assertIsNotNone(self, obj, msg=None)— Included for symmetry with assertIsNone.IsolatedAsyncioTestCase.assertIsSubclass(self, cls, superclass, msg=None)—IsolatedAsyncioTestCase.assertIsSubclassmethod/property.IsolatedAsyncioTestCase.assertLess(self, a, b, msg=None)— Just like self.assertTrue(a < b), but with a nicer default message.IsolatedAsyncioTestCase.assertLessEqual(self, a, b, msg=None)— Just like self.assertTrue(a <= b), but with a nicer default message.IsolatedAsyncioTestCase.assertListEqual(self, list1, list2, msg=None)— A list-specific equality assertion.IsolatedAsyncioTestCase.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.IsolatedAsyncioTestCase.assertMultiLineEqual(self, first, second, msg=None)— Assert that two multi-line strings are equal.IsolatedAsyncioTestCase.assertNoLogs(self, logger=None, level=None)— Fail unless no log messages of level level or higher are emitted on logger_name or its children.IsolatedAsyncioTestCase.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 theIsolatedAsyncioTestCase.assertNotEndsWith(self, s, suffix, msg=None)—IsolatedAsyncioTestCase.assertNotEndsWithmethod/property.IsolatedAsyncioTestCase.assertNotEqual(self, first, second, msg=None)— Fail if the two objects are equal as determined by the '!=' operator.IsolatedAsyncioTestCase.assertNotHasAttr(self, obj, name, msg=None)—IsolatedAsyncioTestCase.assertNotHasAttrmethod/property.IsolatedAsyncioTestCase.assertNotIn(self, member, container, msg=None)— Just like self.assertTrue(a not in b), but with a nicer default message.IsolatedAsyncioTestCase.assertNotIsInstance(self, obj, cls, msg=None)— Included for symmetry with assertIsInstance.IsolatedAsyncioTestCase.assertNotIsSubclass(self, cls, superclass, msg=None)—IsolatedAsyncioTestCase.assertNotIsSubclassmethod/property.IsolatedAsyncioTestCase.assertNotRegex(self, text, unexpected_regex, msg=None)— Fail the test if the text matches the regular expression.IsolatedAsyncioTestCase.assertNotStartsWith(self, s, prefix, msg=None)—IsolatedAsyncioTestCase.assertNotStartsWithmethod/property.IsolatedAsyncioTestCase.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 isIsolatedAsyncioTestCase.assertRaisesRegex(self, expected_exception, expected_regex, *args, **kwargs)— Asserts that the message in a raised exception matches a regex.IsolatedAsyncioTestCase.assertRegex(self, text, expected_regex, msg=None)— Fail the test unless the text matches the regular expression.IsolatedAsyncioTestCase.assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None)— An equality assertion for ordered sequences (like lists and tuples).IsolatedAsyncioTestCase.assertSetEqual(self, set1, set2, msg=None)— A set-specific equality assertion.
Usage
import asyncio
import unittest
class AsyncDemo(unittest.IsolatedAsyncioTestCase):
async def test_sleep(self) -> None:
await asyncio.sleep(0)
self.assertEqual(1 + 1, 2)
suite = unittest.defaultTestLoader.loadTestsFromTestCase(AsyncDemo)
res = unittest.TestResult()
suite.run(res)
assert res.wasSuccessful()
Gotchas
- Fresh event loop per test: Supports
async def asyncSetUp()/asyncTearDown()andaddAsyncCleanup()for clean async resource teardown.
REPL Inspect
from unittest import IsolatedAsyncioTestCase
help(IsolatedAsyncioTestCase)
Linked from
Related notes
- tuts/python3/stdlib_module/unittest/installHandler Linked here; links to this page; shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/main 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/registerResult shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/removeHandler shared tags: #stdlib, #unittest