RuntimeError: Event loop is closed, after first pytest with pytest.mark.anyio
See original GitHub issueHi, 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:
- Created 2 years ago
- Comments:11 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes, I think that was the right call. Let us know, through the channels above, if you need further assistance.
I just set
cache_logger_on_first_use=Falsein the structlog.configure call and the error disappeared.