From fed6fd32d80458219c7d167d529c0a2681858587 Mon Sep 17 00:00:00 2001 From: Matt Wright Date: Mon, 3 Mar 2014 14:14:54 -0500 Subject: [PATCH 1/3] Pass keyword args from `test_client` method to client class constructor --- flask/app.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flask/app.py b/flask/app.py index 2840f426..3d6e2d72 100644 --- a/flask/app.py +++ b/flask/app.py @@ -821,7 +821,7 @@ class Flask(_PackageBoundObject): # without reloader and that stuff from an interactive shell. self._got_first_request = False - def test_client(self, use_cookies=True): + def test_client(self, use_cookies=True, **kwargs): """Creates a test client for this application. For information about unit testing head over to :ref:`testing`. @@ -857,7 +857,7 @@ class Flask(_PackageBoundObject): cls = self.test_client_class if cls is None: from flask.testing import FlaskClient as cls - return cls(self, self.response_class, use_cookies=use_cookies) + return cls(self, self.response_class, use_cookies=use_cookies, **kwargs) def open_session(self, request): """Creates or opens a new session. Default implementation stores all From 44c025f552075175fc8335f327c0e825e05527dc Mon Sep 17 00:00:00 2001 From: Matt Wright Date: Thu, 13 Mar 2014 18:59:59 -0400 Subject: [PATCH 2/3] Add a little documentation regarding kwargs in the `app.test_client()` method --- flask/app.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/flask/app.py b/flask/app.py index 3d6e2d72..050fcb3e 100644 --- a/flask/app.py +++ b/flask/app.py @@ -844,6 +844,20 @@ class Flask(_PackageBoundObject): 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, authentication=None, *args, **kwargs): + FlaskClient.__init__(*args, **kwargs) + self._authentication = authentication + + app.test_client_class = CustomClient + client = app.test_client(authentication='Basic ....') + See :class:`~flask.testing.FlaskClient` for more information. .. versionchanged:: 0.4 From 986b92203a53c735527e53cbe4f6f6f64aa9e9f8 Mon Sep 17 00:00:00 2001 From: Matt Wright Date: Mon, 20 Oct 2014 15:18:38 -0400 Subject: [PATCH 3/3] Add CHANGES entry and versionchanged entry to docstring --- CHANGES | 7 +++++-- flask/app.py | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 9c75f144..5ea2baae 100644 --- a/CHANGES +++ b/CHANGES @@ -8,6 +8,9 @@ Version 1.0 (release date to be announced, codename to be selected) +- Added `**kwargs` to :meth:`flask.Test.test_client` to support passing + additional keyword arguments to the constructor of + :attr:`flask.Flask.test_client_class`. - Added ``SESSION_REFRESH_EACH_REQUEST`` config key that controls the set-cookie behavior. If set to `True` a permanent session will be refreshed each request and get their lifetime extended, if set to @@ -20,8 +23,8 @@ Version 1.0 - Added :meth:`flask.Config.from_json`. - Added :attr:`flask.Flask.config_class`. - Added :meth:`flask.config.Config.get_namespace`. -- Added ``TEMPLATES_AUTO_RELOAD`` config key. If disabled the - templates will be reloaded only if the application is running in +- Added ``TEMPLATES_AUTO_RELOAD`` config key. If disabled the + templates will be reloaded only if the application is running in debug mode. For higher performance it’s possible to disable that. - Added a workaround for a limitation in Python 3.3's namespace loader. - Added support for explicit root paths when using Python 3.3's namespace diff --git a/flask/app.py b/flask/app.py index 050fcb3e..4e97605e 100644 --- a/flask/app.py +++ b/flask/app.py @@ -867,6 +867,10 @@ class Flask(_PackageBoundObject): 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:: 1.0 + 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: