Sessions are empty when testing
See original GitHub issueThis doc expresses that we can access session
without any extra context manager.
However the following code does not return any session key (session is empty):
from flask import Flask
import pytest
from flask import session
myapp = Flask('testing')
myapp.secret_key = 'secret'
myapp.debug = True
@myapp.route('/')
def home():
return session['message']
@pytest.fixture
def app():
return myapp
def test_home(client):
session['message'] = 'something'
response = client.get('/')
assert response.data == 'something'
Any hint on what we’re missing here?
Thanks 😃
Issue Analytics
- State:
- Created 6 years ago
- Reactions:8
- Comments:7 (1 by maintainers)
Top Results From Across the Web
How do check if a PHP session is empty? - Stack Overflow
If you want to check whether sessions are available, you probably want to use the session_id() function: session_id() returns the session id ...
Read more >Sessions seem to always be empty and regenerated - Laracasts
Sessions seem to always be empty and regenerated. I've been playing with cookies on a laravel test app that I keep coming back...
Read more >why in Unit testing current session is empty? - Mendix Forum
One way to test microflows that have dependancies such as the current user is to instead pass it as a parameter to a...
Read more >How do check if a PHP session is empty | Edureka Community
Make sure you're calling session_start before reading from or writing to the session array. Hope it helps!! answered Nov 9, 2020 by ...
Read more >Python: pytest accessing Flask session and request context ...
Sessions are empty when testing #69 raises this problem, and user russmac suggests a solution: @pytest.fixture(scope='module') ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Was also having this issue until I moved my
yield client
outside of thewith client.session_transaction() as session:
enumeration. You can debug by printing the session object, Which shows the secure cookie contents.Wont work:
Works:
Thanks @vinyll for taking the time to open the issue and all others for providing examples and a working solution. I feel like the issue could use a bit more info on why the described problem is happening.
While pytest-flask pushes a test request context for you at the beginning of the test session as stated in the docs
pytest-flask does not prevent flask from creating and pushing new request context objects onto the request context stack upon request handling. What this means and its consequences can be seen with help of the code snippet bellow:
Output:
From this we can see that flask is pushing a new request context into the stack, shadowing the session object of the test request context pushed by pytest-flask and this seems to be the reason for the problem. The solution provided by @russmac is the official flask way of getting around this problem. A secondary possible solution would be using a view function (similar to my example above) to write to the session object and then access it using
session["yourkey"]
within your test since the session object will still be reachable: