Home / Function/ test_client() — flask Function Reference

test_client() — flask Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  1643847b_6492_87aa_076d_8297b1e1e7e6["test_client()"]
  792879e9_430c_854e_d046_ff41a9c758ac["Flask"]
  1643847b_6492_87aa_076d_8297b1e1e7e6 -->|defined in| 792879e9_430c_854e_d046_ff41a9c758ac
  93a9b153_9546_2c3e_8d99_ef02db43e9b7["get()"]
  1643847b_6492_87aa_076d_8297b1e1e7e6 -->|calls| 93a9b153_9546_2c3e_8d99_ef02db43e9b7
  401293cb_88b7_fe8d_ebb9_718382581673["pop()"]
  1643847b_6492_87aa_076d_8297b1e1e7e6 -->|calls| 401293cb_88b7_fe8d_ebb9_718382581673
  style 1643847b_6492_87aa_076d_8297b1e1e7e6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/flask/app.py lines 754–810

    def test_client(self, use_cookies: bool = True, **kwargs: t.Any) -> FlaskClient:
        """Creates a test client for this application.  For information
        about unit testing head over to :doc:`/testing`.

        Note that if you are testing for assertions or exceptions in your
        application code, you must set ``app.testing = True`` in order for the
        exceptions to propagate to the test client.  Otherwise, the exception
        will be handled by the application (not visible to the test client) and
        the only indication of an AssertionError or other exception will be a
        500 status code response to the test client.  See the :attr:`testing`
        attribute.  For example::

            app.testing = True
            client = app.test_client()

        The test client can be used in a ``with`` block to defer the closing down
        of the context until the end of the ``with`` block.  This is useful if
        you want to access the context locals for testing::

            with app.test_client() as c:
                rv = c.get('/?vodka=42')
                assert request.args['vodka'] == '42'

        Additionally, you may pass optional keyword arguments that will then
        be passed to the application's :attr:`test_client_class` constructor.
        For example::

            from flask.testing import FlaskClient

            class CustomClient(FlaskClient):
                def __init__(self, *args, **kwargs):
                    self._authentication = kwargs.pop("authentication")
                    super(CustomClient,self).__init__( *args, **kwargs)

            app.test_client_class = CustomClient
            client = app.test_client(authentication='Basic ....')

        See :class:`~flask.testing.FlaskClient` for more information.

        .. versionchanged:: 0.4
           added support for ``with`` block usage for the client.

        .. versionadded:: 0.7
           The `use_cookies` parameter was added as well as the ability
           to override the client to be used by setting the
           :attr:`test_client_class` attribute.

        .. versionchanged:: 0.11
           Added `**kwargs` to support passing additional keyword arguments to
           the constructor of :attr:`test_client_class`.
        """
        cls = self.test_client_class
        if cls is None:
            from .testing import FlaskClient as cls
        return cls(  # type: ignore
            self, self.response_class, use_cookies=use_cookies, **kwargs
        )

Subdomains

Defined In

Calls

Frequently Asked Questions

What does test_client() do?
test_client() is a function in the flask codebase, defined in src/flask/app.py.
Where is test_client() defined?
test_client() is defined in src/flask/app.py at line 754.
What does test_client() call?
test_client() calls 2 function(s): get, pop.

Analyze Your Own Codebase

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

Try Supermodel Free