Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 单元测试FastAPI积垢不';I don’我没有按预期工作_Unit Testing_Mocking_Pytest_Monkeypatching_Fastapi - Fatal编程技术网

Unit testing 单元测试FastAPI积垢不';I don’我没有按预期工作

Unit testing 单元测试FastAPI积垢不';I don’我没有按预期工作,unit-testing,mocking,pytest,monkeypatching,fastapi,Unit Testing,Mocking,Pytest,Monkeypatching,Fastapi,我对使用FastAPI的项目中的单元测试有问题。 我使用了TestClient、pytest和monkeypatch。 当我测试一个GET函数时,我不知道为什么,但是为了得到模拟函数的结果,结果来自“real”服务,从DB获得结果 要测试的功能: 来自fastapi导入APIRouter,正文 ... ... 路由器=APIRouter() @router.post('/',response_description=“添加到数据库中的功能数据”) 异步定义添加要素数据(要素:要素模式=主体(…)

我对使用FastAPI的项目中的单元测试有问题。 我使用了TestClient、pytest和monkeypatch。 当我测试一个GET函数时,我不知道为什么,但是为了得到模拟函数的结果,结果来自“real”服务,从DB获得结果

要测试的功能:

来自fastapi导入APIRouter,正文
...
...
路由器=APIRouter()
@router.post('/',response_description=“添加到数据库中的功能数据”)
异步定义添加要素数据(要素:要素模式=主体(…):
功能=JSU编码器(功能)
新建功能=等待添加功能(功能)
return ResponseModel(新功能,“功能添加成功”)
我的文件conftest.py

导入pytest
从fastapi.testclient导入testclient
从app.main导入克林贡
@pytest.fixture(scope='module')
def测试应用程序():
client=TestClient(克林贡语)
收益客户
测试文件:

导入json
导入pytest
从app.server.routes导入功能\u路由
def测试获取功能(测试应用程序,monkeypatch):
测试数据=[
{
“更改”:“1”,
‘urs’:‘某物’,
“状态”:“活动”,
“创建时间”:“2020-11-09T11:51:14Z”,
“创建人”:“9”
},
{
“更改”:“2”,
‘urs’:‘某物2’,
“状态”:“活动”,
“创建时间”:“2020-10-16T11:51:14Z”,
“创建人”:“3”
}
]
异步def mock_get_features():
返回测试数据
setattr(特性路由、“获取特性”、“模拟获取特性”)
response=test_app.get('/feature/'))
assert response.status_code==200
assert response.json()==测试数据

谢谢

现在还不太清楚您正在测试的“获取”方法在哪里。。。