bpo-32972: Document IsolatedAsyncioTestCase of unittest module (GH-15… · python/cpython@0ba5dbd (original) (raw)
`@@ -1486,8 +1486,84 @@ Test cases
`
1486
1486
` .. versionadded:: 3.8
`
1487
1487
``
1488
1488
``
``
1489
`+
.. class:: IsolatedAsyncioTestCase(methodName='runTest')
`
1489
1490
``
``
1491
`` +
This class provides an API similar to :class:TestCase
and also accepts
``
``
1492
`+
coroutines as test functions.
`
1490
1493
``
``
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"]``
1491
1567
``
1492
1568
``
1493
1569
`.. class:: FunctionTestCase(testFunc, setUp=None, tearDown=None, description=None)
`