RuntimeError: Event loop is closed, after first pytest with pytest.mark.anyio

See original GitHub issue

Hi, I am trying to implement pytests using anyio. It works for the very first test but after that I get an error saying Event loop is closed. How can I fix that?

import pytest
from httpx import AsyncClient

from wolf.my_namespace.my_package.main import create_app

@pytest.fixture
def anyio_backend() -> str:
    return "asyncio"

client = AsyncClient(app=create_app(), base_url="http://test")


@pytest.mark.anyio
async def test_index() -> None:
    response = await client.get("/")
    assert response.status_code == 200
    assert response.json() == {"hello": "wolf"}


@pytest.mark.anyio
async def test_health() -> None:
    response = await client.get("/health/db")
    assert response.status_code in [200, 503]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
agronholmcommented, Oct 7, 2021

I just set cache_logger_on_first_use=False in the structlog.configure call and the error disappeared.

Yes, I think that was the right call. Let us know, through the channels above, if you need further assistance.

0reactions
bluebrowncommented, Oct 7, 2021

I just set cache_logger_on_first_use=False in the structlog.configure call and the error disappeared.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pytest-asyncio has a closed event loop, but only when running ...
However when I run all tests (pytest at the project root), it fails every time with a runtime error, saying the loop is...
Read more >
Event loop is closed #371 - pytest-dev/pytest-asyncio - GitHub
While running the tests I am receiving these warning errors, and I suspect this causes CircleCI tests to run forever and not finish....
Read more >
Testing with AnyIO — AnyIO 3.6.2 documentation
Pytest does not natively support running asynchronous test functions, so they have to be marked for the AnyIO pytest plugin to pick them...
Read more >
Async Tests - FastAPI
The marker @pytest.mark.anyio tells pytest that this test function should be ... to instantiate objects that need an event loop only within async...
Read more >
How to make sure your asyncio tests always run - Quantlane
The one extra thing you need to do is to always mark your test functions with the @pytest.mark.asyncio decorator. See the following example ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found