Skip to content

Instantly share code, notes, and snippets.

@drocco007
Last active December 16, 2015 16:49
Show Gist options
  • Save drocco007/5466261 to your computer and use it in GitHub Desktop.
Save drocco007/5466261 to your computer and use it in GitHub Desktop.
pytest fixture for SQLAlchemy tests to wrap each test in its own transaction, which is automatically rolled back when the test completes. Drop into a test module to enable for each test in that module. Or, put it into a package's conftest.py to enable it for every test in that package.
@pytest.fixture(autouse=True)
def close_transaction(request):
session.begin(subtransactions=True)
request.addfinalizer(session.rollback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment