October 2018
Intermediate to advanced
420 pages
10h 26m
English
Python does not contain any specific features or modules to test asynchronous code. However, this is not really needed: it is possible to use the unittest module to test AsyncIO code quite easily. Each test can instantiate a new event loop, do the testing on it, and stop the event loop. Once the event loop is stopped, assertions can be done to check whether the test has passed or failed, as can be seen in the following example:
class AsyncIOTestCase(TestCase): def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) def tearDown(self): self.loop.stop() self.loop.close() def test_example(self): future = self.loop.create_future() expected_result = [42] expected_error = None actual_result = ...
Read now
Unlock full access