测试期间模拟环境变量

use*_*151 2 python mocking pytest fastapi pytest-mock

我有一个非常简单的 fastapi 应用程序,我想测试它,代码dummy_api.py如下:

import os
from fastapi import FastAPI


app = FastAPI()


@app.get(os.getenv("ENDPOINT", "/get"))
def func():
    return {
        "message": "Endpoint working !!!"
    }
Run Code Online (Sandbox Code Playgroud)

当我想测试这个时,我使用以下文件:

from fastapi.testclient import TestClient
import dummy_api


def test_dummy_api():
    client = TestClient(dummy_api.app)
    response = client.get("/get")
    assert response.status_code == 200


def test_dummy_api_with_envar(monkeypatch):
    monkeypatch.setenv("ENDPOINT", "dummy")
    client = TestClient(dummy_api.app)
    response = client.get("/dummy")
    assert response.status_code == 200
Run Code Online (Sandbox Code Playgroud)

但是,我无法模拟环境变量部分,因为其中一个测试因404.

pytest -s -v
================================================================= test session starts ==================================================================
platform linux -- Python 3.8.5, pytest-6.2.2, py-1.9.0, pluggy-0.13.1 -- /home/subhayan/anaconda3/envs/fastapi/bin/python
cachedir: .pytest_cache
rootdir: /home/subhayan/Codes/ai4bd/roughdir
collected 2 items                                                                                                                                      

test_dummy_api.py::test_dummy_api PASSED
test_dummy_api.py::test_dummy_api_with_envar FAILED

======================================================================= FAILURES =======================================================================
______________________________________________________________ test_dummy_api_with_envar _______________________________________________________________

monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7ff8c4cf1430>

    def test_dummy_api_with_envar(monkeypatch):
        monkeypatch.setenv("ENDPOINT", "dummy")
        client = TestClient(dummy_api.app)
        response = client.get("/dummy")
>       assert response.status_code == 200
E       assert 404 == 200
E         +404
E         -200

test_dummy_api.py:15: AssertionError
=============================================================== short test summary info ================================================================
FAILED test_dummy_api.py::test_dummy_api_with_envar - assert 404 == 200
============================================================= 1 failed, 1 passed in 0.19s ==============================================================

Run Code Online (Sandbox Code Playgroud)

谁能指出我哪里错了!!

gch*_*bon 7

您可以使用参数化装置importlib.reload函数来测试确实使用了环境变量。

\n

我的测试目录如下所示:

\n
.\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 tests\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 conftest.py\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dummy_api.py\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test_api.py\n
Run Code Online (Sandbox Code Playgroud)\n

这是我的conftest.py

\n
.\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 tests\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 conftest.py\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dummy_api.py\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test_api.py\n
Run Code Online (Sandbox Code Playgroud)\n

这是test_api.py文件:

\n
import pytest\nfrom fastapi.testclient import TestClient\nfrom importlib import reload\nimport dummy_api\n\n\n@pytest.fixture(params=["/get", "/dummy", "/other"])\ndef endpoint(request, monkeypatch):\n    monkeypatch.setenv("ENDPOINT", request.param)\n    return request.param\n\n\n@pytest.fixture()\ndef client(endpoint):\n    app = reload(dummy_api).app\n    yield TestClient(app=app)\n\n
Run Code Online (Sandbox Code Playgroud)\n

运行后测试输出pytest

\n
collected 3 items                                                                                                                                    \n\ntests/test_api.py::test_dummy_api[/get] PASSED                                                                                                   [ 33%]\ntests/test_api.py::test_dummy_api[/dummy] PASSED                                                                                                  [ 66%]\ntests/test_api.py::test_dummy_api[/other] PASSED                                                                                                  [100%]\n
Run Code Online (Sandbox Code Playgroud)\n