如何使用pytest flask将post数据发送到flask 项目设置 Python 3.5.3 烧瓶0.12.2 目录 预期行为
所有路由链接都位于如何使用pytest flask将post数据发送到flask 项目设置 Python 3.5.3 烧瓶0.12.2 目录 预期行为,flask,python-3.5,pytest,Flask,Python 3.5,Pytest,所有路由链接都位于route/RouteManager.py 烧瓶主要来源于index.py 我想使用pytest flask发送假请求和测试响应 实际行为 我不知道如何发送虚假的post数据和测试 来源 index.py 路由/路由管理器.py 测试用例 /上传/ 请求数据 标题 内容类型应用程序/json 身体 { “数据”:[20.0,30.0,401.0,50.0], “日期”:[“2017-08-11”、“2017-08-12”、“2017-08-13”、“2017-08
route/RouteManager.py
index.py
pytest flask
发送假请求和测试响应- 我不知道如何发送虚假的post数据和测试
- 标题
内容类型应用程序/json
- 身体
{
“数据”:[20.0,30.0,401.0,50.0],
“日期”:[“2017-08-11”、“2017-08-12”、“2017-08-13”、“2017-08-14”],
“天”:4
}
响应数据
- 标题
内容类型应用程序/json
- 身体 {“结果”:39}
- 发送虚假的post数据
- 接收响应数据
- 断言检查
不等于结果
-1
def test_upload(client):
mimetype = 'application/json'
headers = {
'Content-Type': mimetype,
'Accept': mimetype
}
data = {
'Data': [20.0, 30.0, 401.0, 50.0],
'Date': ['2017-08-11', '2017-08-12', '2017-08-13', '2017-08-14'],
'Day': 4
}
url = '/upload/'
response = client.post(url, data=json.dumps(data), headers=headers)
assert response.content_type == mimetype
assert response.json['Result'] == 39
虽然接受的答案有效,但可以通过将kwarg
json
传递到post
方法而不是data
def test_upload(client):
data = {
'Data': [20.0, 30.0, 401.0, 50.0],
'Date': ['2017-08-11', '2017-08-12', '2017-08-13', '2017-08-14'],
'Day': 4
}
url = '/upload/'
response = client.post(url, json=data)
assert response.content_type == 'application/json'
assert response.json['Result'] == 39
我建议你使用图书馆。它基于unittest内置库,而且更简单。在那里,您可以键入
response=self.app.post('/route',data=dict(email='xxx',password='xxx'))
获取响应,然后使用self.assertNotIn(b'-1',response.data)
检查它。应该注意,客户端的实例实际上是(这解释了为什么您在pytest flask文档或flask testing文档中都找不到发布的文档)。对于那些坚持使用/支持0.x版本flask的人:您需要接受的答案。虽然现在这确实是一个更好的选择,但json关键字参数直到flask 1.0(2018年4月)才添加。
@routes.route("/")
def IndexPage():
LoggingManager.PrintLogMessage("RouteManager", "IndexPage", "web page connection!", DefineManager.LOG_LEVEL_INFO)
return HomePage.RenderIndexPage()
@routes.route("/upload/", methods=['POST'])
def UploadRawDatas():
content = request.get_json(silent=True)
LoggingManager.PrintLogMessage("RouteManager", "UploadRawDatas", "json data: " + str(content), DefineManager.LOG_LEVEL_INFO)
return BackgroundProcessManager.UploadRawDatas(content['Data'], content['Date'], content['Day'])
@routes.route("/forecast/", methods=['POST'])
def ForecastDatas():
content = request.get_json(silent=True)
LoggingManager.PrintLogMessage("RouteManager", "ForecastDatas", "json data: " + str(content), DefineManager.LOG_LEVEL_INFO)
return BackgroundProcessManager.ForecastDatas(content['ProcessId'])
def test_upload(client):
mimetype = 'application/json'
headers = {
'Content-Type': mimetype,
'Accept': mimetype
}
data = {
'Data': [20.0, 30.0, 401.0, 50.0],
'Date': ['2017-08-11', '2017-08-12', '2017-08-13', '2017-08-14'],
'Day': 4
}
url = '/upload/'
response = client.post(url, data=json.dumps(data), headers=headers)
assert response.content_type == mimetype
assert response.json['Result'] == 39
def test_upload(client):
data = {
'Data': [20.0, 30.0, 401.0, 50.0],
'Date': ['2017-08-11', '2017-08-12', '2017-08-13', '2017-08-14'],
'Day': 4
}
url = '/upload/'
response = client.post(url, json=data)
assert response.content_type == 'application/json'
assert response.json['Result'] == 39