-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect usage of Flask's app context during testing #90
Comments
Don't create an application context during testing. See #90 for more info on the reason why this was problematic. Adjust test fixtures to compensate. BREAKING CHANGE --------------- No longer initializing an app context means that flask.current_app as well as keg.db.db operations may fail where they did not previously. You will need to adjust your test fixtures to either get the app instance directly or create the app context as needed. The changes to our tests in this commit will provide good examples of different kinds of fixture usage that can be used to work around this change.
Don't create an application context during testing. See #90 for more info on the reason why this was problematic. Adjust test fixtures to compensate. BREAKING CHANGE --------------- No longer initializing an app context means that flask.current_app as well as keg.db.db operations may fail where they did not previously. You will need to adjust your test fixtures to either get the app instance directly or create the app context as needed. The changes to our tests in this commit will provide good examples of different kinds of fixture usage that can be used to work around this change.
A lot of our apps assume an app context is going to be available in |
pytest 4.2 will have a fix for us: |
Don't create an application context during testing. See #90 for more info on the reason why this was problematic. Adjust test fixtures to compensate. BREAKING CHANGE --------------- No longer initializing an app context means that flask.current_app as well as keg.db.db operations may fail where they did not previously. You will need to adjust your test fixtures to either get the app instance directly or create the app context as needed. The changes to our tests in this commit will provide good examples of different kinds of fixture usage that can be used to work around this change.
- flask-login now uses flask.g to cache the logged-in user - level12/keg#90 needs to be resolved before we can remove the workaround
This is now affecting both CSRF and how flask-login caches the logged-in user on flask.g. I took a look at this during this maintenance cycle to see if I could wrap this up, and there's a few things at play here that make it non-trivial to finish up. I've punted for this cycle of dependency updates. Taking keg-auth's test suite as a sample, since it doesn't have the same type of custom app setup stuff going on during tests:
|
Don't create an application context during testing. See #90 for more info on the reason why this was problematic. Adjust test fixtures to compensate. BREAKING CHANGE --------------- No longer initializing an app context means that flask.current_app as well as keg.db.db operations may fail where they did not previously. You will need to adjust your test fixtures to either get the app instance directly or create the app context as needed. The changes to our tests in this commit will provide good examples of different kinds of fixture usage that can be used to work around this change.
I haven't read all of your comment above yet but I wanted to point out that I was planning on solving some of the problems with this by using app/db fixtures in the Also, maybe that's not the best solution. |
I'm ending up doing two things:
FWIW, I have the keg-auth tests working locally with these updates and the branch linked above. I think that's at least the first stage in moving this direction, but if I'm missing something, I'm all ears. |
Note, level12/flask-webtest#18 is required for the current solution. |
Our
keg.testing:ContextManager
is designed to cache the application instance as well as the app context. However, after reading the Flask docs on app context, it has become obvious that I'm using the app context wrong. Despite it's name, it's not intended to have the same life cycle as the app instance. It's intended to have a lifetime almost the same as the request context.This is actually the underlying reason behind the bug I thought Flask-WTF had: pallets-eco/flask-wtf#301
It looks like some kind of pytest fixture support is what we really need in order to make the app instance available to our tests instead of relying on
flask.current_app
.Some good ideas here:
https://github.com/pytest-dev/pytest-flask
The text was updated successfully, but these errors were encountered: