Skip to content

Commit 6a9fd66

Browse files
tirkarthimiss-islington
authored andcommitted
bpo-32972: Document IsolatedAsyncioTestCase of unittest module (GH-15878)
* Document `unittest.IsolatedAsyncioTestCase` API * Add a simple example with respect to order of evaluation of setup and teardown calls. https://bugs.python.org/issue32972 Automerge-Triggered-By: @asvetlov
1 parent 7a6873c commit 6a9fd66

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

Doc/library/unittest.rst

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,8 +1486,84 @@ Test cases
14861486
.. versionadded:: 3.8
14871487

14881488

1489+
.. class:: IsolatedAsyncioTestCase(methodName='runTest')
14891490

1491+
This class provides an API similar to :class:`TestCase` and also accepts
1492+
coroutines as test functions.
14901493

1494+
.. versionadded:: 3.8
1495+
1496+
.. coroutinemethod:: asyncSetUp()
1497+
1498+
Method called to prepare the test fixture. This is called after :meth:`setUp`.
1499+
This is called immediately before calling the test method; other than
1500+
:exc:`AssertionError` or :exc:`SkipTest`, any exception raised by this method
1501+
will be considered an error rather than a test failure. The default implementation
1502+
does nothing.
1503+
1504+
.. coroutinemethod:: asyncTearDown()
1505+
1506+
Method called immediately after the test method has been called and the
1507+
result recorded. This is called before :meth:`tearDown`. This is called even if
1508+
the test method raised an exception, so the implementation in subclasses may need
1509+
to be particularly careful about checking internal state. Any exception, other than
1510+
:exc:`AssertionError` or :exc:`SkipTest`, raised by this method will be
1511+
considered an additional error rather than a test failure (thus increasing
1512+
the total number of reported errors). This method will only be called if
1513+
the :meth:`asyncSetUp` succeeds, regardless of the outcome of the test method.
1514+
The default implementation does nothing.
1515+
1516+
.. method:: addAsyncCleanup(function, /, *args, **kwargs)
1517+
1518+
This method accepts a coroutine that can be used as a cleanup function.
1519+
1520+
.. method:: run(result=None)
1521+
1522+
Sets up a new event loop to run the test, collecting the result into
1523+
the :class:`TestResult` object passed as *result*. If *result* is
1524+
omitted or ``None``, a temporary result object is created (by calling
1525+
the :meth:`defaultTestResult` method) and used. The result object is
1526+
returned to :meth:`run`'s caller. At the end of the test all the tasks
1527+
in the event loop are cancelled.
1528+
1529+
1530+
An example illustrating the order::
1531+
1532+
from unittest import IsolatedAsyncioTestCase
1533+
1534+
events = []
1535+
1536+
1537+
class Test(IsolatedAsyncioTestCase):
1538+
1539+
1540+
def setUp(self):
1541+
events.append("setUp")
1542+
1543+
async def asyncSetUp(self):
1544+
self._async_connection = await AsyncConnection()
1545+
events.append("asyncSetUp")
1546+
1547+
async def test_response(self):
1548+
events.append("test_response")
1549+
response = await self._async_connection.get("https://example.com")
1550+
self.assertEqual(response.status_code, 200)
1551+
self.addAsyncCleanup(self.on_cleanup)
1552+
1553+
def tearDown(self):
1554+
events.append("tearDown")
1555+
1556+
async def asyncTearDown(self):
1557+
await self._async_connection.close()
1558+
events.append("asyncTearDown")
1559+
1560+
async def on_cleanup(self):
1561+
events.append("cleanup")
1562+
1563+
if __name__ == "__main__":
1564+
unittest.main()
1565+
1566+
After running the test ``events`` would contain ``["setUp", "asyncSetUp", "test_response", "asyncTearDown", "tearDown", "cleanup"]``
14911567

14921568

14931569
.. class:: FunctionTestCase(testFunc, setUp=None, tearDown=None, description=None)

Doc/whatsnew/3.8.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,32 @@ unittest
11111111
* Several mock assert functions now also print a list of actual calls upon
11121112
failure. (Contributed by Petter Strandmark in :issue:`35047`.)
11131113

1114+
* :mod:`unittest` module gained support for coroutines to be used as test cases
1115+
with :class:`unittest.IsolatedAsyncioTestCase`.
1116+
(Contributed by Andrew Svetlov in :issue:`32972`.)
1117+
1118+
Example::
1119+
1120+
import unittest
1121+
1122+
1123+
class TestRequest(unittest.IsolatedAsyncioTestCase):
1124+
1125+
async def asyncSetUp(self):
1126+
self.connection = await AsyncConnection()
1127+
1128+
async def test_get(self):
1129+
response = await self.connection.get("https://example.com")
1130+
self.assertEqual(response.status_code, 200)
1131+
1132+
async def asyncTearDown(self):
1133+
await self.connection.close()
1134+
1135+
1136+
if __name__ == "__main__":
1137+
unittest.main()
1138+
1139+
11141140
venv
11151141
----
11161142

Misc/NEWS.d/3.8.0b1.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ detect classes that can be passed to `hex()`, `oct()` and `bin()`.
808808
.. nonce: LoeUNh
809809
.. section: Library
810810
811-
Implement ``unittest.AsyncTestCase`` to help testing asyncio-based code.
811+
Implement ``unittest.IsolatedAsyncioTestCase`` to help testing asyncio-based code.
812812

813813
..
814814

0 commit comments

Comments
 (0)