Test Redirection In Flask with Python Unittest

Jason Brooks picture Jason Brooks · Apr 18, 2014 · Viewed 14.5k times · Source

I am currently trying to write some unit tests for my Flask application. In many of my view functions (such as my login), I redirect to a new page. So for example:

@user.route('/login', methods=['GET', 'POST'])
def login():
    ....
    return redirect(url_for('splash.dashboard'))

I'm trying to verify that this redirect happens in my unit tests. Right now, I have:

def test_register(self):
    rv = self.create_user('John','Smith','[email protected]', 'helloworld')
    self.assertEquals(rv.status, "200 OK")
    # self.assert_redirects(rv, url_for('splash.dashboard'))

This function does make sure that the returned response is 200, but the last line is obviously not valid syntax. How can I assert this? My create_user function is simply:

def create_user(self, firstname, lastname, email, password):
        return self.app.post('/user/register', data=dict(
            firstname=firstname,
            lastname=lastname,
            email=email,
            password=password
        ), follow_redirects=True)

Answer

Rachel Sanders picture Rachel Sanders · Apr 18, 2014

Flask has built-in testing hooks and a test client, which works great for functional stuff like this.

from flask import url_for, request
import yourapp

test_client = yourapp.app.test_client()
with test_client:
    response = test_client.get(url_for('whatever.url'), follow_redirects=True)
    # check that the path changed
    assert request.path == url_for('redirected.url')

For older versions of Flask/Werkzeug the request may be available on the response:

from flask import url_for
import yourapp

test_client = yourapp.app.test_client()
response = test_client.get(url_for('whatever.url'), follow_redirects=True)

# check that the path changed
assert response.request.path == url_for('redirected.url')

The docs have more information on how to do this, although FYI if you see "flaskr", that's the name of the test class and not anything in Flask, which confused me the first time I saw it.