<p dir="auto">At flask.testing:81, there exists the block below added in <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/pallets/flask/commit/e97253e4c1a0380f0b70108e8f984b0d9b87ac11/hovercard" href="https://github.com/pallets/flask/commit/e97253e4c1a0380f0b70108e8f984b0d9b87ac11"><tt>e97253e</tt></a>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    if 'json' in kwargs:
        ...
        # push a context so flask.json can use app's json attributes
        with app.app_context():
            kwargs['data'] = json_dumps(kwargs.pop('json'))"><pre class="notranslate"><code class="notranslate">    if 'json' in kwargs:
        ...
        # push a context so flask.json can use app's json attributes
        with app.app_context():
            kwargs['data'] = json_dumps(kwargs.pop('json'))
</code></pre></div>
<p dir="auto">This enables the werkzeug client to use app or blueprint defaults for the JSONEncode to use. However this invokes an app context and consequently (and surprisingly) the handlers handlers registered to the app context lifecycle.</p>
<p dir="auto">Since this behavior only happens when the <code class="notranslate">json</code> kwarg is used on the test client, it can lead to surprising behavior in tests that make use of json and inconsistent behavior between tests that do and do not use json. A more expected behavior would be to have consistent behavior both when and when not using the <code class="notranslate">json</code> kwarg.</p>
<p dir="auto">I also do not see any other usages of the app_context context manager in flask.testing, so this doesn't seem like a pattern that should be expected (not that I'm well versed in Flasks internal patterns, so take that for what its worth).</p>
<p dir="auto">I discovered this because Flask-SQLALchemy registers an app context teardown that removes the session and unexpectedly rolls back a session mid testcase. I imagine this behavior is not unique to Flask-SQLAlchemy.</p>
<p dir="auto">I propose removing the app_context context handler surrounding the <code class="notranslate">json_dumps</code> call, since</p>
<ul dir="auto">
<li><strong>this was the behavior prior to <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/pallets/flask/commit/e97253e4c1a0380f0b70108e8f984b0d9b87ac11/hovercard" href="https://github.com/pallets/flask/commit/e97253e4c1a0380f0b70108e8f984b0d9b87ac11"><tt>e97253e</tt></a></strong></li>
<li>the general flask pattern is already to wrap the higher level calls in an app context, so there is unlikely to be significant impact</li>
<li>json_dumps already degrades gracefully when there is no app context</li>
<li>the behavior change is constrained to the testing utilities</li>
</ul>
<p dir="auto">I can submit the PR for this if there's appetite, since it's just a couple of lines.</p>
<p dir="auto">Python3, flask 1.02.</p>
<p dir="auto">The new blueprint template lookup scheme where the templates folder is just added to the searchpath instead of doing some weird stuff with the names as before. The documentation has to be clearer about that.</p>