小编Kry*_*eyt的帖子

Fastapi 测试 RuntimeError:任务附加到不同的循环

我尝试用 pytest 测试我的端点

主要.py:

from fastapi import FastAPI, status, HTTPException, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from .schema import ClientIn, ClientOut, ClientInWithID, Client, ValidationErrorSchema
from . import clients
from .database import SessionLocal


app = FastAPI()


async def get_db() -> AsyncSession:
    if hasattr(get_db, "db"):
        db: AsyncSession = get_db.db
        return db
    db = SessionLocal()
    setattr(get_db, "db", db)
    return db


@app.post("/client/",
          response_model=ClientOut,
          tags=["client"],
          responses={422: {"model": ValidationErrorSchema}}
          )
async def create_client(client_in: ClientIn, db: AsyncSession = Depends(get_db)) -> Client:
    client = await clients.create_client(db, client_in)
    return client …
Run Code Online (Sandbox Code Playgroud)

python testing asynchronous python-asyncio fastapi

6
推荐指数
1
解决办法
2547
查看次数

标签 统计

asynchronous ×1

fastapi ×1

python ×1

python-asyncio ×1

testing ×1