From 3e67db50f0c1f9bdfb45b8088eecb7e570fa8760 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 2 Jun 2017 16:02:18 +0200 Subject: [PATCH] Tests: added some code to easily enter the Flask app context This can't be trivially enabled globally, since it seems to leak certain things like authentication info between calls. --- pillar/tests/__init__.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pillar/tests/__init__.py b/pillar/tests/__init__.py index 8090a867..9d7e3418 100644 --- a/pillar/tests/__init__.py +++ b/pillar/tests/__init__.py @@ -25,6 +25,7 @@ eve_test_settings.override_eve() from eve.tests import TestMinimal import pymongo.collection from flask.testing import FlaskClient +import flask.ctx import responses import pillar @@ -110,6 +111,9 @@ class AbstractPillarTest(TestMinimal): self.app = self.pillar_server_class(os.path.dirname(os.path.dirname(__file__))) 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' @@ -119,10 +123,22 @@ class AbstractPillarTest(TestMinimal): def tearDown(self): super(AbstractPillarTest, self).tearDown() + if self._app_ctx is not None: + self._app_ctx.__exit__(*sys.exc_info()) + # Not only delete self.app (like the superclass does), # but also un-import the application. self.unload_modules('pillar') + def enter_app_context(self): + """Globally starts an app context. + + The app context is automatically exited upon testcase teardown. + """ + + self._app_ctx: flask.ctx.AppContext = self.app.app_context() + self._app_ctx.__enter__() + def unload_modules(self, module_name): """Uploads the named module, and all submodules."""