简体   繁体   English

FastAPI:app.dependency_overrides 影响其他测试文件

[英]FastAPI: app.dependency_overrides affects other test files

I'm working with FastAPI.我正在使用 FastAPI。 I've introduced in my test suite ( pytest ) app.dependency_overrides to test my app dependencies.我在我的测试套件 ( pytest ) 中引入了app.dependency_overrides来测试我的应用程序依赖项。 Strangely enough, when I use it in a test file, tests in other files start failing.奇怪的是,当我在测试文件中使用它时,其他文件中的测试开始失败。 It looks like app.dependency_overrides affects what happens in other tests.看起来app.dependency_overrides会影响其他测试中发生的事情。

Example:例子:

tests
├── test_1.py
└── test_2.py

test_1.py test_1.py

def foo():
    return "foo"

client = TestClient(app)
 
app.dependency_overrides[get_settings] = foo

def test_1():
    response = client.get("/ping")
    assert response.status_code == 200
    assert response.json() == {"ping": "pong"}

test_2.py test_2.py

client = TestClient(app)

def test_2():
    print(app.dependency_overrides)
    response = client.get("/ping")
    assert response.status_code == 200
    assert response.json() == {"ping": "pong"}

Output Output

tests/test_1.py::test_1 PASSED
tests/test_2.py::test_2 {<functools._lru_cache_wrapper object at 0x10f513cc0>: <function foo at 0x10ea34a60>}
PASSED

As you can see, dependencies overridden in test_1.py are affecting test_2.py .如您所见,在test_1.py中覆盖的依赖项正在影响test_2.py The doc states:该文档指出:

If you want to override a dependency only during some tests, you can set the override at the beginning of the test (inside the test function) and reset it at the end (at the end of the test function).如果您只想在某些测试期间覆盖依赖项,您可以在测试开始时(在测试函数内部)设置覆盖并在结束时(在测试函数结束时)重置它。

I was wondering that such a rule applies intra -module (ie, each test file starts with an empty app.dependency_overrides ), but not inter -module.我想知道这样的规则适用于模块(即,每个测试文件都以空的app.dependency_overrides ),但不适用于模块 Looks like I was wrong.看来我错了。

Is there a way to isolate the effect of app.dependency_overrides to the all the tests in each file without affecting other modules of the test suite?有没有办法隔离app.dependency_overrides对每个文件中所有测试的影响而不影响测试套件的其他模块? (Apart from defining custom app.dependency_overrides in each(!) test function) (除了在每个(!)测试函数中定义自定义app.dependency_overrides

In your code, the statement app.dependency_overrides[get_settings] = foo effects all the tests which are executed after it is evaluated, since there is no cleanup.在您的代码中,语句app.dependency_overrides[get_settings] = foo会影响在评估后执行的所有测试,因为没有清理。

To solve this issue and have a simpler way of writing tests which change FastAPI dependencies, I've created the pytest-fastapi-deps library.为了解决这个问题并以更简单的方式编写更改 FastAPI 依赖项的测试,我创建了pytest-fastapi-deps库。

Use it like so:像这样使用它:

def test_1(fastapi_dep):
    with fastapi_dep(app).override({get_settings: foo}):
        response = client.get("/ping")
        assert response.status_code == 200
        assert response.json() == {"ping": "pong"}

This works for me:这对我有用:


@pytest.fixture(scope="module")
def admin_user():
    """This mocks the oauth authentication in that it returns a value user
    instead of Depending on the actual implementation of oauthRequired
    """
    app.dependency_overrides[oauthRequired] = lambda: OauthUser(
        token="", profile=__MOCKED_USER
    )
    yield app
    del app.dependency_overrides[oauthRequired]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM