I have integration tests using WebApplicationFactory
and TestContainers for hosting Postgres.
This works but spins up a Postgres instance per test, which will not work for my use-case.
I have a single static Postgres instance shared across all my tests.
The obvious problem is tests stepping over each other when modifying data.
My understanding is that I could
- Single thread all my tests and reset the database to an initial state before each test
- Wrap each test in a database transaction.
I have each test init create a database transaction and rollback on cleanup. This all works well.
This issue I have is that I need to modify data within each test via the DbContext object. Since the DbContext object is different between my test and the api I’m testing any changes within my transaction are not reflected in the api’s DbContext.
My question is, is there a way to either grab the DbContext the Api is using?
Any other advice would be much appreciated as well.