Přeskočit na hlavní obsah

Test support Testcontainers PostgreSQL

Overview

Contains support for using a PostgreSQL testcontainer in integration tests. Provides extension methods that wire up a PostgreSqlContainer instance to either another testcontainer's builder or an IConfigurationBuilder, so that the containerised PostgreSQL database is automatically connected to the component under test.

Install

  • ASOL.Core.QA.TestSupport.Testcontainers.PostgreSql

WithPostgreSql (IContainerBuilder)

Use ContainerBuilderExtensions.WithPostgreSql to wire a PostgreSQL testcontainer into another testcontainer that acts as a test tool — for example, a service container spun up as a dependency to support a test. The method injects the PostgreSQL connection string as an environment variable into that testcontainer and declares a startup dependency so the PostgreSQL container is started first.

Note: The PostgreSQL test container does not need to be started before calling this method. The dependency is resolved automatically when the target container starts.

Signature

public static TBuilderEntity WithPostgreSql<TBuilderEntity, TContainerEntity, TConfigurationEntity>(
this IContainerBuilder<TBuilderEntity, TContainerEntity, TConfigurationEntity> builder,
PostgreSqlContainer postgreSqlContainer,
string connectionStringName)
ParameterDescription
builderThe IContainerBuilder for the testcontainer acting as a test tool (e.g. a service container used as a test dependency, not the SUT).
postgreSqlContainerThe PostgreSqlContainer instance to connect to. The container must have a name or a network alias so it can be resolved by the target container over the shared Docker network.
connectionStringNameThe named connection string key used by the application (e.g. "MasterConnectionOptions"). Injected as the environment variable ConnectionStrings__<connectionStringName>.

Example

var network = new NetworkBuilder().Build();

var postgreSqlContainer = new PostgreSqlBuilder("postgres:15")
.WithName($"test-postgresql-{Guid.NewGuid():N}")
.WithNetwork(network)
.Build();

var testApiContainerBuilder = new ContainerBuilder(testApiImage)
.WithNetwork(network)
.WithPortBinding(80, true)
.WithWaitStrategy(Wait.ForUnixContainer().UntilInternalTcpPortIsAvailable(80))
.WithPostgreSql(postgreSqlContainer, "MasterConnectionOptions");

var testAppContainer = testApiContainerBuilder.Build();
await testAppContainer.StartAsync(cancellationToken);

AddPostgreSql (IConfigurationBuilder)

Use ConfigurationBuilderExtensions.AddPostgreSql when the application under test uses standard Core PostgreSql connection patter. The method reads the connection string from the already-running PostgreSQL testcontainer and adds it to the IConfigurationBuilder.

Note: The PostgreSQL test container must be started before calling this method.

Signature

public static IConfigurationBuilder AddPostgreSql(
this IConfigurationBuilder configurationBuilder,
PostgreSqlContainer postgreSqlContainer,
string connectionStringName)
ParameterDescription
configurationBuilderThe configuration builder to add PostgreSQL settings to.
postgreSqlContainerA started PostgreSqlContainer instance.
connectionStringNameThe named connection string key. Added as ConnectionStrings:<connectionStringName>.

Example

// In a WebApplicationFactory override
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration(config =>
{
config.AddPostgreSql(postgreSqlContainer, "MasterConnectionOptions");
});
}