简体   繁体   中英

Flask - how to test POST properly?

Here is a minimalist example on how I am going to test web API. Apparently I get 500 instead of expected assertion. 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. An unhandled exception is exactly that - something wrong with the server.

In order to see the exception, enable debug mode in flask.

app.run(debug=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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