Testcontainers Integration
Prova supports integration testing with Testcontainers for .NET through the Prova.Testcontainers library.
Getting Started
Install the Packages
bashdotnet add package Prova.Testcontainers dotnet add package Testcontainers.PostgreSql # or other modulesPer-Test Container Inherit from
ContainerTestto get a fresh container for every test.csharpusing Prova; using Prova.Testcontainers; using Testcontainers.PostgreSql; using DotNet.Testcontainers.Containers; public class MyDbTest : ContainerTest { protected override IContainer ConfigureContainer() { return new PostgreSqlBuilder() .WithDatabase("testdb") .Build(); } [Fact] public async Task CanConnect() { // Container is started automatically before this test var connString = (Container as PostgreSqlContainer).GetConnectionString(); // ... use connection } }Shared Container (Fixture) Use
ContainerFixtureto share a container across a test class.csharppublic class PostgresFixture : ContainerFixture { protected override IContainer ConfigureContainer() { return new PostgreSqlBuilder().Build(); } public string ConnectionString => (Container as PostgreSqlContainer)?.GetConnectionString(); } public class SharedTest : IClassFixture<PostgresFixture> { private readonly PostgresFixture _fixture; public SharedTest(PostgresFixture fixture) => _fixture = fixture; [Fact] public void Test1() { // _fixture.Container is running } }
Lifecycle Management
- ContainerTest:
StartAsyncis called before each test.DisposeAsyncis called after each test. - ContainerFixture:
StartAsyncis called once before the class.DisposeAsyncis called once after all tests in the class.
Requirements
- Docker execution environment (e.g., Docker Desktop, OrbStack, or a Docker-enabled CI runner).