gpt4 book ai didi

flask - 如何使用 pytest-flask 将发布数据发送到 flask

转载 作者:行者123 更新时间:2023-12-03 15:35:23 28 4
gpt4 key购买 nike

项目设置

  • Python 3.5.3
  • flask 0.12.2

  • 目录
    .
    ├── Core
    │   ├── BackgroundProcessManager.py
    │   ├── FirebaseDatabaseManager.py
    │   ├── LearningManager.py
    │   ├── __init__.py
    │  
    ├── FrontEnd
    │   ├── HomePage.py
    │   ├── __init__.py
    │  
    ├── LICENSE
    ├── README.md
    ├── Route
    │   ├── RouteManager.py
    │   ├── __init__.py
    │  
    ├── Settings
    │   ├── DefineManager.py
    │   ├── __init__.py
    │  
    ├── Utils
    │   ├── LoggingManager.py
    │   ├── __init__.py
    │  
    ├── index.py
    └── runner.sh

    预期行为
  • 所有路由链接在Route/RouteManager.py
  • Flask 主要来源在 index.py
  • 我想使用 pytest-flask 发送假请求和测试响应.

  • 实际行为
  • 我不知道如何发送虚假的帖子数据和测试。

  • 来源

    索引.py
    from flask import Flask
    from Settings import DefineManager
    from Route import *
    import imp
    import sys

    imp.reload(sys)

    app = Flask(__name__)
    app.register_blueprint(routes)

    if __name__ == '__main__':
    app.debug = True
    app.run(host=DefineManager.SERVER_USING_HOST, port=DefineManager.SERVER_USING_PORT)

    路线/RouteManager.py
    @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'])

    测试用例

    /上传/

    请求数据
  • 标题
  • Content-Type application/json
  • 正文
  • {
    "Data": [20.0, 30.0, 401.0, 50.0],
    "Date": ["2017-08-11", "2017-08-12", "2017-08-13", "2017-08-14"],
    "Day": 4
    }

    响应数据
  • 标题
  • Content-Type application/json
  • body

    {“结果”:39}

  • 期待测试过程
  • 发送虚假的帖子数据。
  • 接收响应数据。
  • 断言检查 Result不等于 -1
  • 最佳答案

    pytest-flask 提供 several fixtures ,包括 client一。有了它,你可以创建一个类似于这样的测试函数:

    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

    关于flask - 如何使用 pytest-flask 将发布数据发送到 flask ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45703591/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com