Files
doluedu-fastapi-template/tests/conftest.py

24 lines
637 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""测试基座httpx ASGITransport 进程内测试,不起真实服务。
需要本地 27017 有 mongo测试库 app_test与开发库隔离。
"""
import os
import pytest_asyncio
from httpx import ASGITransport, AsyncClient
os.environ.setdefault("MONGO_DB", "app_test")
from src.main import create_app
@pytest_asyncio.fixture
async def client():
app = create_app()
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as ac:
# 手动触发 lifespanASGITransport 不自动跑 lifespan
async with app.router.lifespan_context(app):
yield ac