代码之家  ›  专栏  ›  技术社区  ›  Amin Ba

fastapi-pytest:如何覆盖接受参数的fastapi依赖项?

  •  0
  • Amin Ba  · 技术社区  · 2 年前

    我有这个小 MCVE 运行良好的FastAPI应用程序:

    # run.py
    import uvicorn
    from fastapi import FastAPI, Depends
    
    app = FastAPI()
    
    
    ###########
    
    def dep_with_arg(inp):
        def sub_dep():
            return inp
        return sub_dep
    
    
    @app.get("/a")
    def a(v: str = Depends(dep_with_arg("a"))):
        return v
    
    
    ###########
    
    
    def dep_without_arg():
        return "b"
    
    
    @app.get("/b")
    def b(v: str = Depends(dep_without_arg)):
        return v
    
    
    ###########
    
    
    def main():
        uvicorn.run(
            "run:app",
            host="0.0.0.0",
            reload=True,
            port=8000,
            workers=1
        )
    
    
    if __name__ == "__main__":
        main()
    

    注意的依赖项之间的差异 /a /b 端点。在中 /a 端点,我创建了一个接受参数的依赖项。当我调用它们时,两个端点都按预期工作。

    我现在尝试在覆盖依赖项的同时测试端点,如下所示:

    from run import app, dep_without_arg, dep_with_arg
    from starlette.testclient import TestClient
    
    
    def test_b():
        def dep_override_for_dep_without_arg():
            return "bbb"
    
        test_client = TestClient(app=app)
        test_client.app.dependency_overrides[dep_without_arg] = dep_override_for_dep_without_arg
        resp = test_client.get("/b")
        resp_json = resp.json()
        assert resp_json == "bbb"
    
    
    ###########
    
    
    def test_a_method_1():
        def dep_override_for_dep_with_arg(inp):
            def sub_dep():
                return "aaa"
    
            return sub_dep
    
        test_client = TestClient(app=app)
        test_client.app.dependency_overrides[dep_with_arg] = dep_override_for_dep_with_arg
        resp = test_client.get(
            "/a",
        )
        resp_json = resp.json()
        assert resp_json == "aaa"
    
    
    def test_a_method_2():
        def dep_override_for_dep_with_arg(inp):
            return "aaa"
    
        test_client = TestClient(app=app)
        test_client.app.dependency_overrides[dep_with_arg] = dep_override_for_dep_with_arg
        resp = test_client.get(
            "/a",
        )
        resp_json = resp.json()
        assert resp_json == "aaa"
    
    
    def test_a_method_3():
        def dep_override_for_dep_with_arg(inp):
            return "aaa"
    
        test_client = TestClient(app=app)
        test_client.app.dependency_overrides[dep_with_arg] = dep_override_for_dep_with_arg("aaa")
        resp = test_client.get(
            "/a",
        )
        resp_json = resp.json()
        assert resp_json == "aaa"
    
    
    def test_a_method_4():
        def dep_override_for_dep_with_arg():
            return "aaa"
    
        test_client = TestClient(app=app)
        test_client.app.dependency_overrides[dep_with_arg] = dep_override_for_dep_with_arg
        resp = test_client.get(
            "/a",
        )
        resp_json = resp.json()
        assert resp_json == "aaa"
    
    

    test_b 按预期通过,但所有其他测试均失败:

    FAILED test.py::test_a_method_1 - AssertionError: assert 'a' == 'aaa'
    FAILED test.py::test_a_method_2 - AssertionError: assert 'a' == 'aaa'
    FAILED test.py::test_a_method_3 - AssertionError: assert 'a' == 'aaa'
    FAILED test.py::test_a_method_4 - AssertionError: assert 'a' == 'aaa'
    

    我应该如何覆盖 dep_with_arg 在上面的例子中?


    相关问题: Overriding FastAPI dependencies that have parameters

    0 回复  |  直到 2 年前