-
Notifications
You must be signed in to change notification settings - Fork 45
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
auto_increment during testing #23
Comments
Having a similar issue here, not sure if related. Table IDs increment between different unit tests. |
don't use hardcoded ids in your tests. try using something like factory boy and then just assert that the id of your created test data object matches after you perform your various operations. |
I've ran into this same issue. As a dumb work around I've been querying down the ID to test against before making an assertion. |
I know this is quite old now but I thought it would be worth pointing out that this is expected behaviour and is a limitation of running tests within transactions. See https://stackoverflow.com/questions/449346/mysql-auto-increment-does-not-rollback for a good explanation. |
In my testing, I currently insert a row to a table that has an
auto-incrementing
,primary_key
,Integer
column calledid
. Even with thedb_session
fixture, every time I run the test, theid
column increments across tests runs. The first time I run the test,id == 1
, but the next time I run the test,id == 2
. Everything else in the test gets rolled back, but theid
count persists. Is there a way to ensure that the count resets across test runs?To get around this, at the beginning of each test that involves auto-incrementing an id, I manually run a SQL command to reset the count. The SQL below is for my Postgresql backend.
I feel like this breaks isolation across unit tests, so I'm hoping there is a better
pytest-flask-sqlalchemy
solution.The text was updated successfully, but these errors were encountered: