No More SQLite - How to Write Tests With EF Core Using TestContainers - Daniel Ward

Learn how to replace SQLite with TestContainers for reliable EF Core integration tests. Run real database containers like PostgreSQL in tests with minimal setup & clean state.

Key takeaways
  • SQLite and in-memory providers can cause false positives/negatives in tests since they don’t exactly match production database behavior

  • TestContainers allows running tests against real database containers (like PostgreSQL) with minimal setup code

  • Key benefits of TestContainers:

    • Uses same database engine as production
    • Handles container lifecycle automatically
    • Provides clean state between test runs
    • Works with multiple database types and services
    • Fast performance even with hundreds of tests
  • Respawn library helps efficiently reset database state between tests by creating foreign key dependency graphs

  • Collection fixtures in xUnit help optimize container reuse across test classes while maintaining isolation

  • Common integration test challenges addressed:

    • Database state cleanup
    • Schema management
    • Port conflicts
    • Container lifecycle management
    • Test parallelization
  • TestContainers includes “Ryuk” resource reaper container to ensure cleanup even if tests crash

  • Setup requires minimal code:

    • One line to create container
    • One line to start it
    • One line to dispose
  • Works well in CI/CD pipelines since it only requires Docker-compatible runtime

  • Knowledge is transferable since TestContainers exists for multiple languages and database types