• FastAPI(61)- 异步测试


    前言

    • 前面讲解了通过 pytest 进行单元测试,是针对同步函数的:https://www.cnblogs.com/poloyy/p/15354901.html
    • 但它无法再 pytest 中测试或运行任何异步函数
    • 能够在测试中使用异步函数可能很有用
    • 例如,当异步查询数据库时,假设想要测试向 FastAPI 应用程序发送请求,然后验证后端是否成功在数据库中写入了正确的数据,同时使用异步数据库

    FastAPI 代码

    from fastapi import FastAPI
    
    app = FastAPI()
    
    
    @app.get("/")
    async def root():
        return {"message": "Tomato"}

    单元测试代码

    需要先安装

    pip install httpx
    pip install trio
    pip install anyio

    测试代码

    import pytest
    from httpx import AsyncClient
    
    from .main import app
    
    
    @pytest.mark.anyio
    async def test_root():
        async with AsyncClient(app=app, base_url="http://test") as ac:
            response = await ac.get("/")
        assert response.status_code == 200
        assert response.json() == {"message": "Tomato"}

    httpx

    • 即使 FastAPI 应用程序使用普通 def 函数而不是 async def,它仍然是一个异步应用程序
    • TestClient 在内部使用标准 pytest 在正常 def 测试函数中调用异步 FastAPI 应用程序做了一些魔术
    • 但是当在异步函数中使用调用异步 FastAPI 应用程序时,这种魔法就不再起作用了
    • 通过异步运行测试用例,不能再在测试函数中使用 TestClient,此时有一个不错的替代方案,称为 HTTPX
    • HTTPX 是 Python 3 的 HTTP 客户端,它允许像使用 TestClient 一样查询 FastAPI 应用程序
    • HTTPX 的 API 和 requests 库几乎相同
    • 重要的区别:用 HTTPX 不仅限于同步,还可以发出异步请求 

    @pytest.mark.anyio

    告诉 pytest 这个测试函数应该异步调用

    AsyncClient

    • 通过使用 FastAPI app 创建一个 AsyncClient,并使用 await 向它发送异步请求
    • 需要搭配 async/await 一起使用
  • 相关阅读:
    随笔——关于读论文
    enumerate
    torch.max
    C# WPF侧边栏导航菜单(Dropdown Menu)
    C# WPF过渡效果实现(C# WPF Material Design UI: Transitions)
    用C# WPF简单实现仪表控件
    自定义滚动条(Custom ScrollBar)
    从头实现一个WPF条形图
    漂亮的无序列表样式
    C# WPF实用的注册窗体
  • 原文地址:https://www.cnblogs.com/poloyy/p/15377148.html
Copyright © 2020-2023  润新知