Enhanced.Testing.Component.PostgreSql
1.4.0
See the version list below for details.
dotnet add package Enhanced.Testing.Component.PostgreSql --version 1.4.0
NuGet\Install-Package Enhanced.Testing.Component.PostgreSql -Version 1.4.0
<PackageReference Include="Enhanced.Testing.Component.PostgreSql" Version="1.4.0" />
<PackageVersion Include="Enhanced.Testing.Component.PostgreSql" Version="1.4.0" />
<PackageReference Include="Enhanced.Testing.Component.PostgreSql" />
paket add Enhanced.Testing.Component.PostgreSql --version 1.4.0
#r "nuget: Enhanced.Testing.Component.PostgreSql, 1.4.0"
#:package Enhanced.Testing.Component.PostgreSql@1.4.0
#addin nuget:?package=Enhanced.Testing.Component.PostgreSql&version=1.4.0
#tool nuget:?package=Enhanced.Testing.Component.PostgreSql&version=1.4.0
Enhanced.Testing.Component.PostgreSql
A PostgreSQL container harness for Enhanced.Testing.Component, with fast non-durable defaults and per-test template databases.
dotnet add package Enhanced.Testing.Component.PostgreSql
Usage
var postgreSql = new PostgreSqlHarness { ConnectionName = "Orders" };
var component = ComponentBuilder.Create<Program>()
.AddHarness(postgreSql)
.AddHarness(http)
.Build();
The application reads ConnectionStrings:Orders and gets the container's connection string.
postgreSql.GetConnectionString() returns it to the test.
| Option | Default | |
|---|---|---|
Image |
postgres:15.1 |
The container image |
Username, Password, Database |
Testcontainers defaults | Credentials and database name |
ConnectionProperties |
empty | Extra connection string properties (escaped correctly) |
Durable |
false |
See below |
ConnectionName, ConfigureContainer and StartConcurrently are shared by all container harnesses; see
Enhanced.Testing.Component.
To create the schema with EF Core and query the application's database, add Enhanced.Testing.Component.DbContext.
Fast, non-durable by default
Test data does not need to survive a crash, so the harness keeps the data directory (PGDATA) on tmpfs and runs with
fsync, full_page_writes and synchronous_commit off. Nothing is written to disk, which reduces IO load when many
containers run in parallel.
Set Durable = true to get the image's durable behaviour instead, for example when a test restarts the server and
expects the data to survive, or when the database is too large to keep in memory:
var postgreSql = new PostgreSqlHarness { Durable = true };
The harness mounts tmpfs at /var/lib/postgresql/data; if ConfigureContainer mounts a volume or bind mount there,
Docker rejects the duplicate mount point, so set Durable = true in that case.
Per-test databases from a template
To isolate tests that write to PostgreSQL without starting a container per test, prepare a template database once and
copy it for each test. CREATE DATABASE ... TEMPLATE copies the database at the storage level, so a copy stays fast
(about 60 ms, mostly docker exec overhead) even when building the schema and seed data takes much longer:
await postgreSql.CreateTemplateAsync("people_template", async connectionString =>
{
var options = new DbContextOptionsBuilder<PeopleDbContext>().UseNpgsql(connectionString).Options;
await using var context = new PeopleDbContext(options);
await context.Database.EnsureCreatedAsync();
});
var connectionString = await postgreSql.CreateDatabaseAsync($"people_{Guid.NewGuid():N}", "people_template");
CreateTemplateAsynccreates the database (dropping an existing one with that name), runs your callback with a non-pooled connection string, then marks the database as a template, blocks new connections to it and closes the open ones. PostgreSQL refuses to copy a template that has open connections.CreateDatabaseAsyncreturns the new database's connection string. Without a template it creates an empty database.DropDatabaseAsyncdrops a database (or a template) and closes its connections.GetConnectionString(database)returns the connection string for any database in the container.
The SQL runs through psql inside the container, so the package doesn't depend on Npgsql. Requires PostgreSQL 13 or
later. Names are quoted, must be at most 63 bytes (PostgreSQL's identifier limit) and are case-sensitive.
Databases live as long as the container, so drop per-test databases with DropDatabaseAsync when a test finishes if
the container is shared by many tests. CreateTemplateAsync drops and re-creates a template with the same name.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
-
net10.0
- Enhanced.Testing.Component (>= 1.4.0)
- Testcontainers.PostgreSql (>= 4.16.0)
-
net11.0
- Enhanced.Testing.Component (>= 1.4.0)
- Testcontainers.PostgreSql (>= 4.16.0)
-
net8.0
- Enhanced.Testing.Component (>= 1.4.0)
- Testcontainers.PostgreSql (>= 4.16.0)
-
net9.0
- Enhanced.Testing.Component (>= 1.4.0)
- Testcontainers.PostgreSql (>= 4.16.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.