【问题标题】:Unable to pass parameters to POST call while testing a Flask app测试 Flask 应用程序时无法将参数传递给 POST 调用
【发布时间】:2019-01-17 07:36:15
【问题描述】:

我有一个使用 unittest 编写和编写测试用例的 Flask Web 应用程序。下面是它如何处理signup 路由

server.py

@app.route('/signup', methods=['GET', 'POST'])
def signup():
    if current_user.__dict__.get('id', None):
        return render_template('login.html')

    if request.method == 'GET':
        return render_template('signup.html')

    else:
        data = request.get_json()
        username = data.get('username', '').lower()
        password = data.get('password', '')
        client = data.get('client', '')['name']
        confirm_password = data.get('confirm_password', '')

在测试文件里面,有注册的辅助方法

test_unittesting.py

def signup(self, username, password, confirm, client):
    return self.app.post(
        '/signup',
        data=json.dumps(dict(username=username, password=password, confirm=confirm, client=client)),
        follow_redirects=True
    )

但它似乎不起作用。 request.get_json() 返回空。参数存在于request.get_data() 而不是request.get_json()。我无法更改辅助方法以支持为路由定义的方法。我必须在测试文件中进行哪些更改才能使其以 request.get_json() 的身份运行?

【问题讨论】:

    标签: python python-3.x flask python-unittest


    【解决方案1】:

    文档描述了请求中可用的属性。在最常见的情况下 request.data 将是空的,因为它被用作后备:

    request.data Contains the incoming request data as string in case it came with a mimetype Flask does not handle.
    
    request.args: the key/value pairs in the URL query string
    request.form: the key/value pairs in the body, from a HTML post form, or JavaScript request that isn't JSON encoded
    request.files: the files in the body, which Flask keeps separate from form. HTML forms must use enctype=multipart/form-data or files will not be uploaded.
    request.values: combined args and form, preferring args if keys overlap
    

    所有这些都是 MultiDict 实例。您可以使用以下方式访问值:

    request.form['name']: use indexing if you know the key exists
    request.form.get('name'): use get if the key might not exist
    request.form.getlist('name'): use getlist if the key is sent multiple times and you want a list of values. get only returns the first value.
    

    【讨论】:

    • 它在通过浏览器访问时工作。我想让它作为一个测试工作。
    • 链接有点帮助。我意识到我需要将单元测试中的 mimetype 配置为“application/json”。但无法弄清楚如何做到这一点。
    • 您还可以使用 request.contentType = 'application/json' 或 request.contentType = JSON_CONTENT_TYPE 等显式设置内容类型
    【解决方案2】:

    mimetype 必须设置为 mimetype='application/json' 作为 post 调用中的附加参数。

    def signup(self, username, password, confirm, client):
        return self.app.post(
            '/signup',
            data=json.dumps(dict(username=username, password=password, confirm=confirm, client=client)),
            follow_redirects=True, mimetype='application/json'
        )
    

    【讨论】:

      【解决方案3】:
      def signup(self, username, password, confirm, client):
          return self.app.post(
              '/signup',
              data=json.dumps(dict(username=username, password=password, confirm=confirm, client=client)), content_type='application/json')
      

      【讨论】:

        猜你喜欢
        • 2021-02-05
        • 1970-01-01
        • 2015-12-05
        • 2012-12-24
        • 2015-09-10
        • 2021-01-05
        • 1970-01-01
        • 2021-01-10
        • 1970-01-01
        相关资源
        最近更新 更多