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

sto*_*es2 6 pytest flask python-3.5

项目设置

  • Python 3.5.3
  • 烧瓶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
Run Code Online (Sandbox Code Playgroud)

预期的行为

  • 所有的路线链接都在 Route/RouteManager.py

  • Flask的主要来源是 index.py

  • 我希望使用发送虚假请求和测试响应pytest-flask.

实际行为

  • 我不知道如何发送假的帖子数据和测试.

资源

index.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)
Run Code Online (Sandbox Code Playgroud)

路线/ 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'])
Run Code Online (Sandbox Code Playgroud)

测试用例

/上传/

请求数据

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

  • 身体

    {"结果":39}

期待测试过程

  • 发送假发布数据.
  • 接收响应数据.
  • 断言检查Result不相等-1

lee*_*ong 15

pytest-flask提供了几个固定装置,包括一个客户装置.有了它,您可以制作类似于此的测试功能:

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
Run Code Online (Sandbox Code Playgroud)

  • 应当注意,客户端实例实际上是[werkzeug.test.Client](http://werkzeug.pocoo.org/docs/0.14/test/#werkzeug.test.Client)(这说明了为什么不这样做)在pytest-flask文档或flask测试文档中都找不到发布的文档)。 (2认同)

Anc*_*nia 11

虽然接受的答案有效,但可以通过将 kwarg 传递jsonpost方法而不是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
Run Code Online (Sandbox Code Playgroud)

  • 对于任何坚持使用/支持 0.x 版本 Flask 的人:您需要接受的答案。虽然这确实是一个更好的选择*现在*,但直到 Flask 1.0(2018 年 4 月)才添加 json 关键字参数。 (2认同)