Skip to content

Instantly share code, notes, and snippets.

@TomFaulkner
Created July 1, 2023 20:58
Show Gist options
  • Save TomFaulkner/ee1859ad041fd857964bc3ec2c342c9b to your computer and use it in GitHub Desktop.
Save TomFaulkner/ee1859ad041fd857964bc3ec2c342c9b to your computer and use it in GitHub Desktop.
Fix FastAPI + some async db + httpx tests having no event loop / different event loop
"""Fixes the following error:
RuntimeError: Task <Task pending name='Task-215' coro=<test_view() running at
{proj}/{test_dir}/test_view.py:23> cb=[_run_until_complete_cb() at /{env_dir}/python3.11/asyncio/base_events.py:180]
> got Future <Future pending> attached to a different loop
"""
import asyncio
from typing import Generator
import pytest
@pytest.fixture(scope="session")
def event_loop() -> Generator:
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment