Home / Function/ test_request_context() — flask Function Reference

test_request_context() — flask Function Reference

Architecture documentation for the test_request_context() function in app.py from the flask codebase.

Entity Profile

Dependency Diagram

graph TD
  c6ee720e_217f_24d8_42d3_da38bfeebb45["test_request_context()"]
  9f8cc56e_d5c4_e7a8_438a_3124c0dd5de5["Flask"]
  c6ee720e_217f_24d8_42d3_da38bfeebb45 -->|defined in| 9f8cc56e_d5c4_e7a8_438a_3124c0dd5de5
  eb7c3517_582e_6dd2_c80b_a8be784317c7["session_transaction()"]
  eb7c3517_582e_6dd2_c80b_a8be784317c7 -->|calls| c6ee720e_217f_24d8_42d3_da38bfeebb45
  ba0faaf2_249e_8d3d_df7c_6a7b65513741["request_context()"]
  c6ee720e_217f_24d8_42d3_da38bfeebb45 -->|calls| ba0faaf2_249e_8d3d_df7c_6a7b65513741
  style c6ee720e_217f_24d8_42d3_da38bfeebb45 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/flask/app.py lines 1498–1545

    def test_request_context(self, *args: t.Any, **kwargs: t.Any) -> AppContext:
        """Create an :class:`.AppContext` with request information created from
        the given arguments. When the context is pushed, :data:`.request`,
        :data:`.session`, :data:`g:, and :data:`.current_app` become available.

        This is useful during testing to run a function that uses request data
        without dispatching a full request. Use this as a ``with`` block to push
        a context.

        .. code-block:: python

            with app.test_request_context(...):
                generate_report()

        See :doc:`/appcontext`.

        Takes the same arguments as Werkzeug's
        :class:`~werkzeug.test.EnvironBuilder`, with some defaults from
        the application. See the linked Werkzeug docs for most of the
        available arguments. Flask-specific behavior is listed here.

        :param path: URL path being requested.
        :param base_url: Base URL where the app is being served, which
            ``path`` is relative to. If not given, built from
            :data:`PREFERRED_URL_SCHEME`, ``subdomain``, :data:`SERVER_NAME`,
            and :data:`APPLICATION_ROOT`.
        :param subdomain: Subdomain name to prepend to :data:`SERVER_NAME`.
        :param url_scheme: Scheme to use instead of
            :data:`PREFERRED_URL_SCHEME`.
        :param data: The request body text or bytes,or a dict of form data.
        :param json: If given, this is serialized as JSON and passed as
            ``data``. Also defaults ``content_type`` to
            ``application/json``.
        :param args: Other positional arguments passed to
            :class:`~werkzeug.test.EnvironBuilder`.
        :param kwargs: Other keyword arguments passed to
            :class:`~werkzeug.test.EnvironBuilder`.
        """
        from .testing import EnvironBuilder

        builder = EnvironBuilder(self, *args, **kwargs)

        try:
            environ = builder.get_environ()
        finally:
            builder.close()

        return self.request_context(environ)

Subdomains

Defined In

Frequently Asked Questions

What does test_request_context() do?
test_request_context() is a function in the flask codebase, defined in src/flask/app.py.
Where is test_request_context() defined?
test_request_context() is defined in src/flask/app.py at line 1498.
What does test_request_context() call?
test_request_context() calls 1 function(s): request_context.
What calls test_request_context()?
test_request_context() is called by 1 function(s): session_transaction.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free