The test client now properly pops response contexts on __exit__
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent c8ec453 · Browse files · 4 files·+48 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+48 added−4 removed
- other:+48 added−4 removed
Provenance
- Commit
d49221b- Tree
64e5454- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
c8ec453
4 files changed+48 added−4 removed
modified:docs/upgrading.rst+5 added
34 unmodified lines35 longer have to handle that error to avoid an internal server error showing36 up for the user. If you were catching this down explicitly in the past37 as `ValueError` you will need to change this.38 39+Due to a bug in the test client Flask 0.7 did not trigger teardown40+handlers when the test client was used in a with statement. This was41+since fixed but might require some changes in your testsuites if you42+relied on this behavior.43+44 Version 0.745 -----------46 47 In Flask 0.7 we cleaned up the code base internally a lot and did some277 unmodified linesmodified:flask/testsuite/testing.py+32 added
124 unmodified lines125 pass126 else:127 raise AssertionError('some kind of exception expected')128 129+ def test_reuse_client(self):130+ app = flask.Flask(__name__)131+ c = app.test_client()132 133+ with c:134+ self.assert_equal(c.get('/').status_code, 404)135+136+ with c:137+ self.assert_equal(c.get('/').status_code, 404)138+139+ def test_test_client_calls_teardown_handlers(self):140+ app = flask.Flask(__name__)141+ called = []142+ @app.teardown_request143+ def remember(error):144+ called.append(error)145+146+ with app.test_client() as c:147+ self.assert_equal(called, [])148+ c.get('/')149+ self.assert_equal(called, [])150+ self.assert_equal(called, [None])151+152+ del called[:]153+ with app.test_client() as c:154+ self.assert_equal(called, [])155+ c.get('/')156+ self.assert_equal(called, [])157+ c.get('/')158+ self.assert_equal(called, [None])159+ self.assert_equal(called, [None, None])160+161+162 def suite():163 suite = unittest.TestSuite()164 suite.addTest(unittest.makeSuite(TestToolsTestCase))165 return suitemodified:flask/testing.py+9 added-4 removed
84 unmodified lines85 headers = resp.get_wsgi_headers(c.request.environ)86 self.cookie_jar.extract_wsgi(c.request.environ, headers)87 88 def open(self, *args, **kwargs):89- if self.context_preserved:90- _request_ctx_stack.pop()91- self.context_preserved = False89+ self._pop_reqctx_if_necessary()90 kwargs.setdefault('environ_overrides', {}) \91 ['flask._preserve_context'] = self.preserve_context92 93 as_tuple = kwargs.pop('as_tuple', False)17 unmodified lines111 return self112 113 def __exit__(self, exc_type, exc_value, tb):114 self.preserve_context = False115+ self._pop_reqctx_if_necessary()116+117+ def _pop_reqctx_if_necessary(self):118 if self.context_preserved:119+ # we have to use _request_ctx_stack.top.pop instead of120+ # _request_ctx_stack.pop since we want teardown handlers121+ # to be executed.118- _request_ctx_stack.pop()122+ _request_ctx_stack.top.pop()123+ self.context_preserved = Falsemodified:CHANGES+2 added
38 unmodified lines39 variable as well as ``SERVER_NAME`` are now properly used by the test client40 as defaults.41 - Added :attr:`flask.views.View.decorators` to support simpler decorating of42 pluggable (class based) views.43+- Fixed an issue where the test client if used with the with statement did not44+ trigger the execution of the teardown handlers.45 46 Version 0.7.347 -------------48 238 unmodified lines