Added support for deferred context cleanup. test_client users can now access the context locals after the actual request if the client is used with a with-block. This fixes #59.
This commit is contained in:
parent
937360877b
commit
bc00fd1e83
5 changed files with 111 additions and 3 deletions
17
docs/api.rst
17
docs/api.rst
|
|
@ -300,3 +300,20 @@ Useful Internals
|
|||
all the context local objects used in Flask. This is a documented
|
||||
instance and can be used by extensions and application code but the
|
||||
use is discouraged in general.
|
||||
|
||||
.. versionchanged:: 0.4
|
||||
|
||||
The request context is automatically popped at the end of the request
|
||||
for you. In debug mode the request context is kept around if
|
||||
exceptions happen so that interactive debuggers have a chance to
|
||||
introspect the data. With 0.4 this can also be forced for requests
|
||||
that did not fail and outside of `DEBUG` mode. By setting
|
||||
``'flask._preserve_context'`` to `True` on the WSGI environment the
|
||||
context will not pop itself at the end of the request. This is used by
|
||||
the :meth:`~flask.Flask.test_client` for example to implement the
|
||||
deferred cleanup functionality.
|
||||
|
||||
You might find this helpful for unittests where you need the
|
||||
information from the context local around for a little longer. Make
|
||||
sure to properly :meth:`~werkzeug.LocalStack.pop` the stack yourself in
|
||||
that situation, otherwise your unittests will leak memory.
|
||||
|
|
|
|||
|
|
@ -218,3 +218,27 @@ All the other objects that are context bound can be used the same.
|
|||
If you want to test your application with different configurations and
|
||||
there does not seem to be a good way to do that, consider switching to
|
||||
application factories (see :ref:`app-factories`).
|
||||
|
||||
|
||||
Keeping the Context Around
|
||||
--------------------------
|
||||
|
||||
.. versionadded:: 0.4
|
||||
|
||||
Sometimes it can be helpful to trigger a regular request but keep the
|
||||
context around for a little longer so that additional introspection can
|
||||
happen. With Flask 0.4 this is possible by using the
|
||||
:meth:`~flask.Flask.test_client` with a `with` block::
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
with app.test_client() as c:
|
||||
rv = c.get('/?foo=42')
|
||||
assert request.args['foo'] == '42'
|
||||
|
||||
If you would just be using the :meth:`~flask.Flask.test_client` without
|
||||
the `with` block, the `assert` would fail with an error because `request`
|
||||
is no longer available (because used outside of an actual request).
|
||||
Keep in mind however that :meth:`~flask.Flask.after_request` functions
|
||||
are already called at that point so your database connection and
|
||||
everything involved is probably already closed down.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue