Skip to content
Myr

Updated documentation regarding the session transactions

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 311ac0f · Browse files · 1 file+32 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+32 added−0 removed

  • other:+32 added−0 removed

1 file+32 added−0 removed

modified:docs/testing.rst+32 added
271 unmodified lines
272is no longer available (because you are trying to use it outside of the actual request).273However, keep in mind that any :meth:`~flask.Flask.after_request` functions274are already called at this point so your database connection and275everything involved is probably already closed down.276277278Accessing and Modifying Sessions279--------------------------------280281.. versionadded:: 0.8282283Sometimes it can be very helpful to access or modify the sessions from the284test client. Generally there are two ways for this. Ify ou just want to285ensure that a session has certain keys set to certain values you can just286keep the context around and access :data:`flask.session`::287288 with app.test_client() as c:289 rv = c.get('/')290 assert flask.session['foo'] == 42291292This however does not make it possible to also modify the session or to293access the session before a request was fired. Starting with Flask 0.8 we294provide a so called “session transaction” which simulates the appropriate295calls to open a session in the context of the test client and to modify296it. At the end of the transaction the session is stored. This works297independently of the session backend used::298299 with app.test_client() as c:300 with c.session_transaction() as sess:301 sess['a_key'] = 'a value'302303 # once this is reached the session was stored304305Note that in this case you have to use the ``sess`` object instead of the306:data:`flask.session` proxy. The object however itself will provide the307same interface.