import os import sys _ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) sys.path.insert(0, _ROOT) os.environ.setdefault("ENVIRONMENT", "dev") os.environ["TESTING"] = "1" os.environ["DATABASE_TYPE"] = "sqlite" os.environ["DATABASE_NAME"] = os.path.join(_ROOT, "pytest_fastapiadmin") import pytest from fastapi.testclient import TestClient from sqlalchemy.ext.asyncio import AsyncSession from main import create_app from app.core.database import async_engine from app.core.base_model import MappedBase from app.plugin.module_payment.enterprise.model import EnterpriseModel @pytest.fixture(scope="session") def test_client(): """创建测试客户端""" app = create_app() with TestClient(app) as client: yield client @pytest.fixture(scope="session", autouse=True) def setup_database(): """初始化测试数据库表结构""" import asyncio async def _setup(): async with async_engine.begin() as coon: await coon.run_sync(MappedBase.metadata.create_all) asyncio.get_event_loop().run_until_complete(_setup()) yield asyncio.get_event_loop().run_until_complete(async_engine.dispose())