I'm trying to unittest an application using pytest and an app factory, but I can't seem to get access to the client session object in my tests. I'm sure there's some context I'm not pushing somewhere. I push the app context in my 'app' fixture. Should I push the request context somewhere?
The following is an MWE.
mwe.py:
from flask import Flask, session
def create_app():
app = Flask(__name__)
app.secret_key = 'top secret'
@app.route('/set')
def session_set():
session['key'] = 'value'
return 'Set'
@app.route('/check')
def session_check():
return str('key' in session)
@app.route('/clear')
def session_clear():
session.pop('key', None)
return 'Cleared'
return app
if __name__ == "__main__":
mwe = create_app()
mwe.run()
conftest.py:
import pytest
from mwe import create_app
@pytest.fixture(scope='session')
def app(request):
app = create_app()
ctx = app.app_context()
ctx.push()
def teardown():
ctx.pop()
request.addfinalizer(teardown)
return app
@pytest.fixture(scope='function')
def client(app):
return app.test_client()
test_session.py:
import pytest
from flask import session
def test_value_set_for_client_request(client): # PASS
client.get('/set')
r = client.get('/check')
assert 'True' in r.data
def test_value_set_in_session(client): # FAIL
client.get('/set')
assert 'key' in session
def test_value_set_in_session_transaction(client): # FAIL
with client.session_transaction() as sess:
client.get('/set')
assert 'key' in sess
Note that running this directly works fine, I can jump around /set, /check, /clear and it behaves as expected. Similarly, the test that uses only the test client to GET the pages works as expected. Accessing the session directly however doesn't seem to.
Aucun commentaire:
Enregistrer un commentaire