I'm writing a unit test module for my python3 flask project. One of the tests is intended to check if after passing user credentials to log-in form, the user gets successfuly redirected to home page. The application itself works fine through the web browser.
I have the following snippets:
routes.py:
@users.route('/login', methods=['GET', 'POST'])
def login():
...
# correct credentials
login_user(user, remember=form.remember.data)
return redirect(url_for('main.home'))
# incorrect credentials
flash('Log-in failed! Check your credentials.', 'danger')
return render_template('login.html', title='Login', form=form)
test.py:
from flaskblog import create_app, db
from flask_testing import TestCase
import unittest
from flask import url_for
class Tests(TestCase):
def create_app(self):
return create_app()
def setUp(self):
self.app = self.create_app()
def tearDown(self):
pass
def test_login(self):
tester = self.app.test_client(self)
# log with user credentials
response = tester.post('/login', data={'email': 'ab@abc.com', 'password': '1'})
self.assertRedirects(response, url_for('main.home'))
if __name__ == '__main__':
unittest.main()
When running the test snippet, I keep getting the assertion error:
self.assertTrue(response.status_code in valid_status_codes, message or not_redirect)
AssertionError: False is not true : HTTP Status 301, 302, 303, 305, 307 expected but got 200
This means that the code returned by the server is 200, while I hoped to recieve 302, which would indicate a redirect.
I tried checking the server behavior and that is the result from console when i manually do the log-in:
My question is: how exactly can I check if the redirect is happening?