Question

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','John.Smith@myschool.edu', '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)
Was it helpful?

Solution 2

Try Flask-Testing

there is api for assertRedirects you can use this

assertRedirects(response, location)

Checks if response is an HTTP redirect to the given location.
Parameters: 

    response – Flask response
    location – relative URL (i.e. without http://localhost)

TEST script:

def test_register(self):
    rv = self.create_user('John','Smith','John.Smith@myschool.edu', 'helloworld')
    assertRedirects(rv, url of splash.dashboard)

OTHER TIPS

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.

One way is to not follow the redirects (either remove follow_redirects from your request, or explicitly set it to False).

Then, you can simply replace self.assertEquals(rv.status, "200 OK") with:

self.assertEqual(rv.status_code, 302)
self.assertEqual(rv.location, url_for('splash.dashboard', _external=True))

If you want to continue using follow_redirects for some reason, another (slightly brittle) way is to check for some expected dashboard string, like an HTML element ID in the response of rv.data. e.g. self.assertIn('dashboard-id', rv.data)

You can verify the final path after redirects by using Flask test client as a context manager (using the with keyword). It allows keeping the final request context around in order to import the request object containing request path.

from flask import request, url_for

def test_register(self):
    with self.app.test_client() as client:
        user_data = dict(
            firstname='John',
            lastname='Smith',
            email='John.Smith@myschool.edu',
            password='helloworld'
        )
        res = client.post('/user/register', data=user_data, follow_redirects=True)
        assert res.status == '200 OK'
        assert request.path == url_for('splash.dashboard')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top