Creates a new asyncio event loop based on the current event loop policy. The new loop
is available as the return value of this fixture or via asyncio.get_running_loop.
The event loop is closed when the fixture scope ends. The fixture scope defaults
to function
scope.
def test_http_client(event_loop):
url = "http://httpbin.org/get"
resp = event_loop.run_until_complete(http_client(url))
assert b"HTTP/1.1 200 OK" in resp
Note that, when using the event_loop
fixture, you need to interact with the event loop using methods like event_loop.run_until_complete
. If you want to await code inside your test function, you need to write a coroutine and use it as a test function. The asyncio marker
is used to mark coroutines that should be treated as test functions.
If your tests require an asyncio event loop with class or module scope, apply the asyncio_event_loop mark to the respective class or module.
If you need to change the type of the event loop, prefer setting a custom event loop policy over redefining the event_loop
fixture.
If the pytest.mark.asyncio
decorator is applied to a test function, the event_loop
fixture will be requested automatically by the test function.
Finds and yields a single unused TCP port on the localhost interface. Useful for binding temporary test servers.
A callable which returns a different unused TCP port each invocation. Useful when several unused TCP ports are required in a test.
def a_test(unused_tcp_port_factory):
port1, port2 = unused_tcp_port_factory(), unused_tcp_port_factory()
...
Works just like their TCP counterparts but returns unused UDP ports.