Method: flask.app.Flask.test_client
Calls: 246, Exceptions: 0, Paths: 1Back
Path 1: 246 calls (1.0)
True (245) False (1)
{} (246)
FlaskClient (246)
1def test_client(self, use_cookies: bool = True, **kwargs: t.Any) -> FlaskClient:
2 """Creates a test client for this application. For information
3 about unit testing head over to :doc:`/testing`.
4
5 Note that if you are testing for assertions or exceptions in your
6 application code, you must set ``app.testing = True`` in order for the
7 exceptions to propagate to the test client. Otherwise, the exception
8 will be handled by the application (not visible to the test client) and
9 the only indication of an AssertionError or other exception will be a
10 500 status code response to the test client. See the :attr:`testing`
11 attribute. For example::
12
13 app.testing = True
14 client = app.test_client()
15
16 The test client can be used in a ``with`` block to defer the closing down
17 of the context until the end of the ``with`` block. This is useful if
18 you want to access the context locals for testing::
19
20 with app.test_client() as c:
21 rv = c.get('/?vodka=42')
22 assert request.args['vodka'] == '42'
23
24 Additionally, you may pass optional keyword arguments that will then
25 be passed to the application's :attr:`test_client_class` constructor.
26 For example::
27
28 from flask.testing import FlaskClient
29
30 class CustomClient(FlaskClient):
31 def __init__(self, *args, **kwargs):
32 self._authentication = kwargs.pop("authentication")
33 super(CustomClient,self).__init__( *args, **kwargs)
34
35 app.test_client_class = CustomClient
36 client = app.test_client(authentication='Basic ....')
37
38 See :class:`~flask.testing.FlaskClient` for more information.
39
40 .. versionchanged:: 0.4
41 added support for ``with`` block usage for the client.
42
43 .. versionadded:: 0.7
44 The `use_cookies` parameter was added as well as the ability
45 to override the client to be used by setting the
46 :attr:`test_client_class` attribute.
47
48 .. versionchanged:: 0.11
49 Added `**kwargs` to support passing additional keyword arguments to
50 the constructor of :attr:`test_client_class`.
51 """
52 cls = self.test_client_class
53 if cls is None:
54 from .testing import FlaskClient as cls
55 return cls( # type: ignore
56 self, self.response_class, use_cookies=use_cookies, **kwargs
57 )