I'm trying to set up pytests for my Flask-User based application but struggling to figure out how to log in as a user to test my routes.
Current test_app.py
import pytest
@pytest.fixture(scope='module')
def app():
from app import create_app
app = create_app()
yield app
@pytest.fixture(scope='module')
def client(app):
with app.test_client() as client:
yield client
def test_logged_in_member(client):
# Log in as the 'member' user
response = client.post('/user/sign-in', data={
'username': 'member',
'password': 'Password1'
}, follow_redirects=True) # Enable automatic redirect following
assert response.status_code == 200
assert b'You have signed in successfully.' in response.data
I would expect this to follow the redirect back to the home page where the success message is displayed, but I get this response back from pytest:
test_app.py::test_logged_in_member failed: client = <FlaskClient <Flask 'app'>>
def test_logged_in_member(client):
# Log in as the 'member' user
response = client.post('/user/sign-in', data={
'username': 'member',
'password': 'Password1'
}, follow_redirects=True) # Enable automatic redirect following
assert response.status_code == 200
> assert b'You have signed in successfully.' in response.data
E assert b'You have signed in successfully.' in b'<!DOCTYPE html>\n<html lang="en">\n <head>\n <meta charset="utf-8">\n <meta http-equiv="X-UA-Compatible" cont...ript src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>\n\n \n \n\n </body>\n</html>'
E + where b'<!DOCTYPE html>\n<html lang="en">\n <head>\n <meta charset="utf-8">\n <meta http-equiv="X-UA-Compatible" cont...ript src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>\n\n \n \n\n </body>\n</html>' = <WrapperTestResponse 3942 bytes [200 OK]>.data
test_app.py:75: AssertionError
This response data doesn't look right to me, and I can't figure out where the data is coming from, so either it's not redirecting correctly or my request is just nonsense.
Ideally I want to be in a position to run a test as one of the two users already in the sqlite database to check access levels on the various routes, but as I say I can't figure out how to persist a log in. Any Flask wizards able to help me out here?
Many Thanks,