Top AI Repos — open-source AI, indexed and scored
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
A pytest plugin for preserving test isolation in Flask-SQLAlchemy using database transactions.
| Date | Stars |
|---|---|
| 2026-07-24 | 253 |
| 2026-07-25 | 253 |
| 2026-07-28 | 253 |
| 2026-07-30 | 253 |
| 2026-09-20 | 253 |
Today
— stars today
This week
— stars this week
This month
— stars this month
Momentum
0.0
growth rate 0.00%/day
# pytest-flask-sqlalchemy
A [pytest](https://docs.pytest.org/en/latest/) plugin providing fixtures for running tests in
transactions using [Flask-SQLAlchemy](http://flask-sqlalchemy.pocoo.org/latest/).
## Contents
- [**Motivation**](#motivation)
- [**Quick examples**](#quick-examples)
- [**Usage**](#usage)
- [Installation](#installation)
- [From PyPi](#from-pypi)
- [Development version](#development-version)
- [Supported backends](#supported-backends)
- [Configuration](#configuration)
- [Conftest setup](#conftest-setup)
- [Test configuration](#test-configuration)
- [`mocked-engines`](#mocked-engines)
- [`mocked-sessions`](#mocked-sessions)
- [`mocked-sessionmakers`](#mocked-sessionmakers)
- [Writing transactional tests](#writing-transactional-tests)
- [Fixtures](#fixtures)
- [`db_session`](#db_session)
- [`db_engine`](#db_engine)
- [Enabling transactions without fixtures](#enabling-transactions-without-fixtures)
- [**Development**](#development)
- [Running the tests](#running-the-tests)
- [Acknowledgements](#acknowledgements)
- [Copyright](#copyright)
## <a name="motivation"></a>Motivation
Inspired by [Django's built-in support for transactional
tests](https://jeancochrane.com/blog/django-test-transactions), this plugin
seeks to provide comprehensive, easy-to-use Pytest fixtures for wrapping tests in
database transactions for [Flask-SQLAlchemy](http://flask-sqlalchemy.pocoo.org/latest/)
apps. The goal is to make testing stateful Flask-SQLAlchemy applications easier by
providing fixtures that permit the developer to **make arbitrary database updates
with the confidence that any changes made during a test will roll back** once the test exits.
## <a name="quick-examples"></a>Quick examples
Use the [`db_session` fixture](#db_session) to make **database updates that won't persist beyond
the body of the test**:
```python
def test_a_transaction(db_session):
row = db_session.query(Table).get(1)
row.name = 'testing'
db_session.add(row)
db_session.commit()
def test_transaction_doesnt_persist(db_session):
row = db_session.query(Table).get(1)
assert row.name != 'testing'
```
The [`db_engine` fixture](#db_engine) works the same way, but **copies the API of
SQLAlchemy's [Engine
object](http://docs.sqlalchemy.org/en/latest/core/connections.html#sqlalchemy.engine.Engine)**:
```python
def test_a_transaction_using_engine(db_engine):
with db_engine.begin() as conn:
row = conn.execute('''UPDATE table SET name = 'testing' WHERE id = 1''')
def test_transaction_doesnt_persist(db_engine):
row_name = db_engine.execute('''SELECT name FROM table WHERE id = 1''').fetchone()[0]
assert row_name != 'testing'
```
Use [configuration properties](#test-configuration) to
**mock database connections in an app and enforce nested transactions**,
allowing any method from the codebase to run inside a test with the assurance
that any database changes made will be rolled back at the end of the test:
```ini
# In setup.cfg
[tool:pytest]
mocked-sessions=database.db.session
mocked-engines=database.engine
```
```python
# In database.py
db = flask_sqlalchemy.SQLAlchemy()
engine = sqlalchemy.create_engine('DATABASE_URI')
```
```python
# In models.py
class Table(db.Model):
__tablename__ = 'table'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
def set_name(new_name):
self.name = new_name
db.session.add(self)
db.session.commit()
```
```python
# In tests/test_set_name.py
def test_set_name(db_session):
row = db_session.query(Table).get(1)
row.set_name('testing')
assert row.name == 'testing'
def test_transaction_doesnt_persist(db_session):
row = db_session.query(Table).get(1)
assert row.name != 'testing'
```
# <a name="usage"></a>Usage
## <a name="installation"></a>Installation
### <a name="from-pypi"Excerpt of 19,046 characters
Read on GitHubWould you bet a product on this? Bounded 0–100 and slow moving.
matched fp:484b7e54cc6a804e, topic:testing