Testing errors when streaming
See original GitHub issueI currently have a problem with errors when I try to test a streaming endpoint, namely when I use the stream_with_context
flask function/decorator to keep the context around, as I am streaming from a database which client is on current_app
.
Essentially, the test passes, but then pytest-flask errors with a message about it having popped the wrong request context:
AssertionError: Popped wrong request context. (<RequestContext 'http://localhost/stream' [GET] of app> instead of <RequestContext 'http://localhost/' [GET] of app>)
Or at least I think it’s pytest-flask.
I have a small gist that demonstrates the issue: https://gist.github.com/cknv/152d81ffcc4a74be491d, though it might be a little on the primitive side, but for me it does demonstrate the error. If you need some more information, I would of course be more than happy to provide it 😃
For completeness sake, I am running python 3.5.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:7
- Comments:9 (1 by maintainers)
Top GitHub Comments
Is this “fix” still working? I am running into the same problem here and creating a fixture and explicitly pushing the context is not working for me.
A fix was previously merged in to Flask, but I’m now reverting that. The behavior described here is correct. The context for
stream_with_context
is preserved separately fromwith client
, and should remain as long as the generator is still running. Your test either needs to readresponse.data
, or callresponse.close()
. Either of those will close the generator and release the context. (This might not have been the case at the time it was reported, I’ve made other changes to how the test client handles the response since then.)This issue should be closed, it’s not an issue with Pytest-Flask or Flask.