MCPcopy
hub / github.com/pallets/flask / test_client

Method test_client

src/flask/app.py:669–725  ·  view source on GitHub ↗

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 propag

(self, use_cookies: bool = True, **kwargs: t.Any)

Source from the content-addressed store, hash-verified

667 self._got_first_request = False
668
669 def test_client(self, use_cookies: bool = True, **kwargs: t.Any) -> FlaskClient:
670 """Creates a test client for this application. For information
671 about unit testing head over to :doc:`/testing`.
672
673 Note that if you are testing for assertions or exceptions in your
674 application code, you must set ``app.testing = True`` in order for the
675 exceptions to propagate to the test client. Otherwise, the exception
676 will be handled by the application (not visible to the test client) and
677 the only indication of an AssertionError or other exception will be a
678 500 status code response to the test client. See the :attr:`testing`
679 attribute. For example::
680
681 app.testing = True
682 client = app.test_client()
683
684 The test client can be used in a ``with`` block to defer the closing down
685 of the context until the end of the ``with`` block. This is useful if
686 you want to access the context locals for testing::
687
688 with app.test_client() as c:
689 rv = c.get('/?vodka=42')
690 assert request.args['vodka'] == '42'
691
692 Additionally, you may pass optional keyword arguments that will then
693 be passed to the application's :attr:`test_client_class` constructor.
694 For example::
695
696 from flask.testing import FlaskClient
697
698 class CustomClient(FlaskClient):
699 def __init__(self, *args, **kwargs):
700 self._authentication = kwargs.pop("authentication")
701 super(CustomClient,self).__init__( *args, **kwargs)
702
703 app.test_client_class = CustomClient
704 client = app.test_client(authentication='Basic ....')
705
706 See :class:`~flask.testing.FlaskClient` for more information.
707
708 .. versionchanged:: 0.4
709 added support for ``with`` block usage for the client.
710
711 .. versionadded:: 0.7
712 The `use_cookies` parameter was added as well as the ability
713 to override the client to be used by setting the
714 :attr:`test_client_class` attribute.
715
716 .. versionchanged:: 0.11
717 Added `**kwargs` to support passing additional keyword arguments to
718 the constructor of :attr:`test_client_class`.
719 """
720 cls = self.test_client_class
721 if cls is None:
722 from .testing import FlaskClient as cls
723 return cls( # type: ignore
724 self, self.response_class, use_cookies=use_cookies, **kwargs
725 )
726

Calls

no outgoing calls

Tested by

no test coverage detected