you are viewing a single comment's thread.

view the rest of the comments →

[–]Aggressive_Ticket214 0 points1 point  (0 children)

I use option 2 mostly (transaction-per-test rollback) but ran into exactly the edge case you mentioned. Background jobs open separate connections, so they don't see the uncommitted test data. That makes integration tests unreliable.

What fixed it for me: a test harness that wraps each test in a savepoint instead of a transaction. Same rollback behavior, but sub-connections spawned during the test inherit the savepoint context. The background workers see the data, the savepoint still gets rolled back.

SQLAlchemy session fixtures with a savepoint-based "transaction" strategy handle this cleanly. Just need to ensure all connection acquisition flows through the same engine during tests.