Interview frequency: …
Short answer
It runs a real Docker database container during tests, applies migrations, and destroys it afterward, avoiding differences between InMemory and a real database.
Explanation
Using EF Core InMemory Database in tests can be dangerous because it does not support real Transactions, does not check Foreign Keys, and cannot run Raw SQL or database-specific features. Testcontainers starts a real SQL container matching production in seconds, increasing confidence in queries, indexes, and constraints.
Code example
public class DatabaseFixture : IAsyncLifetime {
private readonly MsSqlContainer _container = new MsSqlBuilder().Build();
public string ConnectionString => _container.GetConnectionString();
public async Task InitializeAsync() => await _container.StartAsync();
public async Task DisposeAsync() => await _container.DisposeAsync();
}Common mistakes
- Starting a new container for every test instead of sharing it through IClassFixture and clearing tables with Respawn.