Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access session object during unittest #18

Closed
thadeusb opened this issue Apr 27, 2010 · 5 comments
Closed

Access session object during unittest #18

thadeusb opened this issue Apr 27, 2010 · 5 comments

Comments

@thadeusb
Copy link

Can we get the docs updated to include an example to access session object while unit testing.

Here is a potential solution, using unittest for flaskr

def test_session(self):
    with flaskr.app.test_request_context('/login', method='POST', data=dict(
                    username=flaskr.USERNAME, password=flaskr.PASSWORD,)):
        rv = flaskr.app.dispatch_request()
        assert flaskr.session['logged_in'] == True

    with flaskr.app.test_request_context('/login', method='POST', data=dict(
                    username=flaskr.USERNAME+'x', password=flaskr.PASSWORD+'x',)):
        rv = flaskr.app.dispatch_request()
        assert not flaskr.session.get('logged_in')
@mitsuhiko
Copy link
Contributor

I do not recommend triggering the request handling like this. The nicer solution is the test_client like the example tests do: http://github.com/mitsuhiko/flask/blob/master/examples/minitwit/minitwit_tests.py

The big advantage is that it does not depend on a certain way to dispatch requests and tests will continue to work if one refactors the request dispatching.

@cgrinds
Copy link
Contributor

cgrinds commented Apr 28, 2010

I assume that using test_client there is no way to access the session since test_client treats the app as a black box. That was what thadeusb was trying to do.

@mitsuhiko
Copy link
Contributor

That is indeed the case but you can hook in a temporary view so that you can access the session from there (for example). There are other ways as well but I'm not sure if its not a better idea to test the application as blackbox in general.

@thadeusb
Copy link
Author

I figured the purpose of unittesting was to make sure your application was working properly.

What if I store a fairly large object in session (say I am making a ampache clone) and I store the playlist. I need to verify that that playlist is correct in the session object, I can't even look at it correctly from an html stand point.

There is another issue with black-box testing, what if I change the way my flash messsage is worded? Well now all my tests are broken, even though my application works just fine and dandy.

@mitsuhiko
Copy link
Contributor

Added example for context bound objects to the testing docs. This closed by 8d49440

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants