title: Flask + SQLAlchemy + pytest - not rolling back my session
body: <p>There are several similar questions on stack overflow, and I apologize in advance if I'm breaking etiquette by asking another one, but I just cannot seem to come up with the proper set of incantations to make this work.</p>
  
  <p>I'm trying to use Flask + Flask-SQLAlchemy and then use pytest to manage the session such that when the function-scoped pytest fixture is torn down, the current transation is rolled back.</p>
  
  <p>Some of the other questions seem to advocate using the db "drop all and create all" pytest fixture at the function scope, but I'm trying to use the joined session, and use rollbacks, since I have a LOT of tests. This would speed it up considerably.</p>
  
  <p><a href="http://alexmic.net/flask-sqlalchemy-pytest/" rel="nofollow noreferrer">http://alexmic.net/flask-sqlalchemy-pytest/</a> is where I found the original idea, and <a href="https://stackoverflow.com/questions/28526781/isolating-py-test-db-sessions-in-flask-sqlalchemy">Isolating py.test DB sessions in Flask-SQLAlchemy</a> is one of the questions recommending using function-level db re-creation.</p>
  
  <p>I had also seen <a href="https://github.com/mitsuhiko/flask-sqlalchemy/pull/249" rel="nofollow noreferrer">https://github.com/mitsuhiko/flask-sqlalchemy/pull/249</a> , but that appears to have been released with flask-sqlalchemy 2.1 (which I am using).</p>
  
  <p>My current (very small, hopefully immediately understandable) repo is here:
  <a href="https://github.com/hoopes/flask-pytest-example" rel="nofollow noreferrer">https://github.com/hoopes/flask-pytest-example</a></p>
  
  <p>There are two print statements - the first (in example/__init__.py) should have an Account object, and the second (in test/conftest.py) is where I expect the db to be cleared out after the transaction is rolled back.</p>
  
  <p>If you <code>pip install -r requirements.txt</code> and run <code>py.test -s</code> from the test directory, you should see the two print statements.</p>
  
  <p>I'm about at the end of my rope here - there must be something I'm missing, but for the life of me, I just can't seem to find it.</p>
  
  <p>Help me, SO, you're my only hope!</p>