Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing session object during Unit test of Flask application

I know it is possible to create session object using session_transaction() method. However, is there a way to access the current session object which gets created when for example "/" route gets hit? I did from flask import session to access the session but it's empty. Let me know if it is possible. Thanks.

like image 374
mrsan22 Avatar asked Mar 29 '17 21:03

mrsan22


1 Answers

This is what you're looking for. As it says however, you'd have to use the instantiation you create in your with statement.

with app.test_client() as c:
    with c.session_transaction() as sess:
        sess['a_key'] = 'a value'

    # once this is reached the session was stored
    result = app.test_client.get('/a_url')
    # NOT part of the 2nd context

Note that this won't work if you run your test within the scope of the with c.session_transaction() as sess statement, it needs to be run after that block.

like image 76
Allie Fitter Avatar answered Oct 19 '22 05:10

Allie Fitter