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
importpytest@pytest.mark.fixture# 👈👈 I think @pytest.fixture (there is no @pytest.mark.fixture...)defapp_ctx(app):
withapp.app_context():
yield@pytest.mark.usefixtures("app_ctx")deftest_user_model(app):
user=User()
db.session.add(user)
db.session.commit()
Regardless of the fact that this is typo or not.
The section itself is really helpful for me to set up test code.
Thanks a lot for adding this section (relatively) recently.
There are already many blogs and youtubes about pytest, flask, sqlalchemy..
But there is no simple, easy to understand ways to set up test environment about flask-sqlalchemy(flask and database)
set up test
If I understand the code above correctly, the environment about database
(such as table, data(rows) in table) is effective across all test functions.
That is, if test_add_row() is executed and then test_row_exists() is executed
former test's result affect latter test's result. Is it right?
That's cool, however, is there any way to set up database indpendent across test functions?
I think it's a lot related to pytest, not flask-sqlalchemy itself. (sorry for wrong place if that is true).
I did some googling and find open source named skylines. In that codebase, there are code related to this topic.
@pytest.fixture(scope="session")defdb(app):
"""Creates clean database schema and drops it on teardown Note, that this is a session scoped fixture, it will be executed only once and shared among all tests. Use `db_session` fixture to get clean database before each test. """assertisinstance(app, SkyLines)
setup_db(app)
yielddatabase.dbteardown_db()
@pytest.fixture(scope="function")defdb_session(db, app):
"""Provides clean database before each test. After each test, session.rollback() is issued. Return sqlalchemy session. """assertisinstance(app, SkyLines)
withapp.app_context():
clean_db()
yielddb.sessiondb.session.rollback()
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
-
Hi, all.
flask-sqlalchemy is awesome.
But I am not awsome and (still) a beginner.
Sorry for this kind of silly question.
docs say in section about test that
(https://flask-sqlalchemy.palletsprojects.com/en/3.0.x/contexts/#tests)
Regardless of the fact that this is typo or not.
The section itself is really helpful for me to set up test code.
Thanks a lot for adding this section (relatively) recently.
There are already many blogs and youtubes about pytest, flask, sqlalchemy..
But there is no simple, easy to understand ways to set up test environment about flask-sqlalchemy(flask and database)
If I understand the code above correctly, the environment about database
(such as table, data(rows) in table) is effective across all test functions.
That is, if
test_add_row()
is executed and thentest_row_exists()
is executedformer test's result affect latter test's result. Is it right?
That's cool, however, is there any way to set up database indpendent across test functions?
I think it's a lot related to pytest, not flask-sqlalchemy itself. (sorry for wrong place if that is true).
I did some googling and find open source named skylines. In that codebase, there are code related to this topic.
(https://github.com/skylines-project/skylines/blob/master/tests/conftest.py#L48)
I think it's helpful too for mentioning about "per test function" and "applied to all test functions" for beginner like me.
Anyway, really appreciate for maintaining this project. It's really awesome !!
(and sorry for my poor English writing)
Beta Was this translation helpful? Give feedback.
All reactions