Skip to content

PY-TEST0009 · synchronous_test_asyncio_run_count

Count asyncio.run calls owned by synchronous pytest tests.

This is a deterministic rule for python. Read its implementation.

Resolve ordinary and aliased imports of asyncio.run in modules matching pytest’s default collection conventions. Report each direct call owned by a synchronous collected test. Calls inside a nested helper scope are independent and are not attributed to the test.

Each finding identifies the collected test and exact runner call. The rule does not match an unrelated local function named run or a same-named parameter that shadows an import. The value is the number of runner calls owned by a synchronous collected test.

asyncio.run remains appropriate at an application entry point. In pytest, prefer an async test owned by AnyIO or pytest-asyncio so the plugin controls event-loop isolation, fixtures, cancellation, and cleanup. Ruff’s PT rules do not check event-loop ownership.

def test_fetch():
response = asyncio.run(fetch())
assert response.status == 200
@pytest.mark.anyio
async def test_fetch():
response = await fetch()
assert response.status == 200
  • Cites “The Python Standard Library”, asyncio runners. Open reference
  • Cites “AnyIO documentation”, asynchronous tests. Open reference
  • Cites “pytest-asyncio documentation”, test discovery modes. Open reference