Skip to content

add test isolation manager - #32

Merged
davidism merged 1 commit into
mainfrom
test-isolation
Nov 6, 2025
Merged

add test isolation manager#32
davidism merged 1 commit into
mainfrom
test-isolation

Conversation

@davidism

@davidism davidism commented Nov 6, 2025

Copy link
Copy Markdown
Member

Add a test_isolation context manager method (and async variant). This turns the code previously described in the "Avoid Writing Data" section of the testing docs into a context manager. The code was pretty complex, this hides that in a simple with block for users. Now the code is much shorter:

@pytest.fixture
def app(monkeypatch):
    app = create_app({
        "SQLALCHEMY_ENGINES": {"default": "postgresql:///project-test"}
    })

    with db.test_isolation():
        yield app

This also works with unittest:

class TestDatabase(unittest.TestCase):
    def setUp(self):
        self.enterContext(db.test_isolation())

Any changes to the database within the with block will be rolled back when it exits.

Our tests use SQLite, and we call out in the docs that you don't need this isolation code and can just blow away the in-memory database for each test in that case. However, it does work with SQLite, but SQLite's transaction handling needs to be fixed: https://docs.sqlalchemy.org/en/20/dialects/sqlite.html#sqlite-transactions. So our tests serve as a demonstration for how to use this with SQLite.

@davidism davidism added this to the 0.2.0 milestone Nov 6, 2025
@davidism
davidism merged commit 54143e7 into main Nov 6, 2025
9 checks passed
@davidism
davidism deleted the test-isolation branch November 6, 2025 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant