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
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.
event_loop fixture can be overridden in any of the standard pytest locations,
e.g. directly in the test file, or in
conftest.py. This allows redefining the
fixture scope, for example:
@pytest.fixture(scope="session") def event_loop(): policy = asyncio.get_event_loop_policy() loop = policy.new_event_loop() yield loop loop.close()
If you need to change the type of the event loop, prefer setting a custom event loop policy over redefining the
pytest.mark.asyncio decorator is applied to a test function, the
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.