I am trying to create a Flask application with Flask-SQLAlchemy; I use pytest to test the db. One of the problems is to create separate database sessions between different tests.
I have prepared a minimal, complete example to highlight the problem, note that test_user_schema1() and test_user_schema2() same.
File name: test_db.py
from models import User def test_user_schema1(session): person_name = 'Fran Clan' uu = User(name=person_name) session.add(uu) session.commit() assert uu.id==1 assert uu.name==person_name def test_user_schema2(session): person_name = 'Stan Clan' uu = User(name=person_name) session.add(uu) session.commit() assert uu.id==1 assert uu.name==person_name
If db is really isolated between my tests, both tests must pass. However, the last test always fails, because I did not find a way to do rollbacks of db sessions correctly.

conftest.py uses the following, based on what I saw in Alex Michael's blog post , but this device code breaks because it seems t to isolate db sessions between devices.
@pytest.yield_fixture(scope='function') def session(app, db): connection = db.engine.connect() transaction = connection.begin()
For the purposes of this question, I built a gist that contains everything you need to reproduce; you can clone it with git clone https://gist.github.com/34fa8d274fc4be240933.git .
I use the following packages ...
Flask==0.10.1 Flask-Bootstrap==3.3.0.1 Flask-Migrate==1.3.0 Flask-Moment==0.4.0 Flask-RESTful==0.3.1 Flask-Script==2.0.5 Flask-SQLAlchemy==2.0 Flask-WTF==0.11 itsdangerous==0.24 pytest==2.6.4 Werkzeug==0.10.1
Two questions:
- Why is the status quo violated? This same py.test tool seemed to work for someone else.
- How can I fix this to work correctly?
source share