You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
According to a discussion in the flask issue queue this will create problems for test methods that also emit test requests to the flask app. In this case the test-wide app context and with it the scoped session is used both in the test method (and its fixtures) and inside the test requests. This means the state of the session is leaked between the test method and the test request which makes the tests behave subtly different than the live environment.
An example is:
@app.route("/test")deftest():
page=model.Page.query.get(1)
page.visit_count+=1# Forgot to commit here so nothing is saved to the database.deftest_visit_count(app_ctx):
page=model.Page(visit_count=0)
db.session.add(page)
db.session.commit()
client=app.test_client()
client.get("/test")
assertpage.visit_count==1
The recommendation of avoiding test-wide app contexts (flask) contradicts the recommendation to use one (flask-sqlalchemy).
How would a good (aka DRY) way of working with models in tests / fixtures look that doesn’t need a test-wide app context?
Does the flask-sqlalchemy documentation need updating?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
The documentation recommends to use a test-wide app context for convenience when many interactions with the DB are needed:
According to a discussion in the flask issue queue this will create problems for test methods that also emit test requests to the flask app. In this case the test-wide app context and with it the scoped session is used both in the test method (and its fixtures) and inside the test requests. This means the state of the session is leaked between the test method and the test request which makes the tests behave subtly different than the live environment.
An example is:
The recommendation of avoiding test-wide app contexts (flask) contradicts the recommendation to use one (flask-sqlalchemy).
Beta Was this translation helpful? Give feedback.
All reactions