我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用flask.testing.FlaskClient()。
def test_client(self): """Creates a test client for this application. For information about unit testing head over to :ref:`testing`. 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' .. versionchanged:: 0.4 added support for `with` block usage for the client. """ from flask.testing import FlaskClient return FlaskClient(self, self.response_class, use_cookies=True)
def test_client(self, use_cookies=True): """Creates a test client for this application. For information about unit testing head over to :ref:`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' 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. """ 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)
def _find_error_handler(self, e): """Finds a registered error handler for the request’s blueprint. Otherwise falls back to the app, returns None if not a suitable handler is found. """ exc_class, code = self._get_exc_class_and_code(type(e)) def find_handler(handler_map): if not handler_map: return for cls in exc_class.__mro__: handler = handler_map.get(cls) if handler is not None: # cache for next time exc_class is raised handler_map[exc_class] = handler return handler # try blueprint handlers handler = find_handler(self.error_handler_spec .get(request.blueprint, {}) .get(code)) if handler is not None: return handler # fall back to app handlers return find_handler(self.error_handler_spec[None].get(code))
def setup_client(before_create_app: Callable = None) -> FlaskClient: """Create and return the testing client. Arguments: before_create_app: A function which will be called before the Flask app is created. Originally implemented to support creating resources for testing. """ # We need a secret key for JWT to work! config.SECRET_KEY = 'loltestingkey' # We don't wanna use redis for testing (too much of a hassle) config.FLASK_CACHE['CACHE_TYPE'] = 'simple' config.TESTING = True config.RATELIMIT_STORAGE_URL = 'memory://' # We'll be using unauthed/free a lot RATELIMIT_DEFAULT = config.RATELIMIT_DEFAULT_FREE config.RATELIMIT_DEFAULT = '2000 per minute' if before_create_app: before_create_app() app = create_app() client = app.test_client() with app.app_context(): users.create_db_and_default_roles() users.create_dummy_users() return client
def setUp(self, **kwargs): eve_settings_file = os.path.join(MY_PATH, 'eve_test_settings.py') kwargs['settings_file'] = eve_settings_file os.environ['EVE_SETTINGS'] = eve_settings_file super(AbstractPillarTest, self).setUp(**kwargs) from eve.utils import config config.DEBUG = True self.app = self.pillar_server_class(pathlib.Path(__file__).parents[2]) self.assertEqual(self.app.config['STORAGE_DIR'], self._pillar_storage_dir) # Run self.enter_app_context() to create this context. self._app_ctx: flask.ctx.AppContext = None self.app.process_extensions() assert self.app.config['MONGO_DBNAME'] == 'pillar_test' self.client = self.app.test_client() assert isinstance(self.client, FlaskClient)