简体   繁体   English

烧瓶-如何正确测试POST?

[英]Flask - how to test POST properly?

Here is a minimalist example on how I am going to test web API. 这是有关如何测试Web API的极简示例。 Apparently I get 500 instead of expected assertion. 显然我得到了500个而不是预期的断言。 What do I do wrong? 我做错了什么?

import unittest

from flask import Flask, request

from app.forms import RegistrationForm

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def hello_world():
    form = RegistrationForm(request.form)
    return form.foo.data+form.bar.data


class AppTestCase(unittest.TestCase):
    def setUp(self):
        self.app = app.test_client()

    def tearDown(self):
        pass

    def test_login_logout(self):
        rv=self.app.post('/', data=dict(
            foo='foooo',
            bar='barrr'
        ), follow_redirects=True)
        assert 'foooobarrr' in rv.data

if __name__ == '__main__':
    app.run()

Assertion raises an exception. 断言引发异常。 Exception details are never sent to the client. 异常详细信息永远不会发送到客户端。 The browser only gets a HTTP 500, which means something is wrong with the server. 浏览器仅获得HTTP 500,这意味着服务器出了点问题。 An unhandled exception is exactly that - something wrong with the server. 未处理的异常恰恰是-服务器出了点问题。

In order to see the exception, enable debug mode in flask. 为了查看异常,请在flask中启用调试模式。

app.run(debug=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM