Skip to content

Commit 624c647

Browse files
author
Jonathan Como
authored
Let Flask manage its own request contexts (#108)
* Let Flask manage its own request contexts * Fix tests for python 3
1 parent afc7659 commit 624c647

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

flask_testing/utils.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,6 @@ def _pre_setup(self):
148148

149149
self.client = self.app.test_client()
150150

151-
self._ctx = self.app.test_request_context()
152-
self._ctx.push()
153-
154151
if not self.render_templates:
155152
# Monkey patch the original template render with a empty render
156153
self._original_template_render = templating._render
@@ -174,10 +171,6 @@ def _add_template(self, app, template, context):
174171
self.templates.append((template, context))
175172

176173
def _post_teardown(self):
177-
if getattr(self, '_ctx', None) is not None:
178-
self._ctx.pop()
179-
del self._ctx
180-
181174
if getattr(self, 'app', None) is not None:
182175
if getattr(self, '_orig_response_class', None) is not None:
183176
self.app.response_class = self._orig_response_class
@@ -438,15 +431,10 @@ def __call__(self, result=None):
438431
self._configured_port = self.app.config.get('LIVESERVER_PORT', 5000)
439432
self._port_value = multiprocessing.Value('i', self._configured_port)
440433

441-
# We need to create a context in order for extensions to catch up
442-
self._ctx = self.app.test_request_context()
443-
self._ctx.push()
444-
445434
try:
446435
self._spawn_live_server()
447436
super(LiveServerTestCase, self).__call__(result)
448437
finally:
449-
self._post_teardown()
450438
self._terminate_live_server()
451439

452440
def get_server_url(self):
@@ -542,11 +530,6 @@ def _get_server_address(self):
542530

543531
return host, port
544532

545-
def _post_teardown(self):
546-
if getattr(self, '_ctx', None) is not None:
547-
self._ctx.pop()
548-
del self._ctx
549-
550533
def _terminate_live_server(self):
551534
if self._process:
552535
self._process.terminate()

tests/flask_app/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
render_template,
88
url_for,
99
flash,
10-
request
10+
request,
11+
g
1112
)
1213

1314

@@ -20,6 +21,14 @@ def create_app():
2021
def index():
2122
return Response("OK")
2223

24+
@app.route("/context")
25+
def context():
26+
if hasattr(g, "thing"):
27+
return "ALREADY SET"
28+
29+
g.thing = True
30+
return "SET"
31+
2332
@app.route("/template/")
2433
def index_with_template():
2534
return render_template("index.html", name="test")

tests/test_utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def create_app(self):
1616
def test_setup(self):
1717
self.assertTrue(self.app is not None)
1818
self.assertTrue(self.client is not None)
19-
self.assertTrue(self._ctx is not None)
2019

2120

2221
class TestSetupFailure(TestCase):
@@ -40,7 +39,6 @@ def test_remove_testcase_attributes(self):
4039
"""
4140

4241
del self.app
43-
del self._ctx
4442

4543
class TestClientUtils(TestCase):
4644

@@ -83,6 +81,12 @@ def test_assert_405(self):
8381
def test_assert_500(self):
8482
self.assert500(self.client.get("/internal_server_error/"))
8583

84+
def test_new_context_used_per_request(self):
85+
response = self.client.get("/context")
86+
self.assertEqual(response.data, b"SET")
87+
response = self.client.get("/context")
88+
self.assertEqual(response.data, b"SET")
89+
8690
def test_assert_redirects(self):
8791
response = self.client.get("/redirect/")
8892
self.assertRedirects(response, "/")

0 commit comments

Comments
 (0)