Switchknife.Testing.Postgres
0.1.0
dotnet add package Switchknife.Testing.Postgres --version 0.1.0
NuGet\Install-Package Switchknife.Testing.Postgres -Version 0.1.0
<PackageReference Include="Switchknife.Testing.Postgres" Version="0.1.0" />
<PackageVersion Include="Switchknife.Testing.Postgres" Version="0.1.0" />
<PackageReference Include="Switchknife.Testing.Postgres" />
paket add Switchknife.Testing.Postgres --version 0.1.0
#r "nuget: Switchknife.Testing.Postgres, 0.1.0"
#:package Switchknife.Testing.Postgres@0.1.0
#addin nuget:?package=Switchknife.Testing.Postgres&version=0.1.0
#tool nuget:?package=Switchknife.Testing.Postgres&version=0.1.0
Switchknife.Testing.Postgres
A PostgreSQL server in a Testcontainers container for integration tests: started once per run, migrated by your callback, and either emptied between tests or split into a database per test. No dependency on a test framework or on Switchknife.Migrations: a fixture of 5 lines wraps it in xUnit 2, xUnit v3 or anything else.
var postgres = new PostgresDatabase("postgres:18-alpine", connectionString =>
new DatabaseMigrator(connectionString, typeof(Program).Assembly, NullLogger.Instance).Run());
await postgres.StartAsync(); // starts the container, then runs the callback once
// postgres.ConnectionString, postgres.DataSource
await postgres.ResetAsync(); // between tests: empties every table, keeps the migration journal
await postgres.DisposeAsync(); // removes the container
The fixture, xUnit v3 (IAsyncLifetime returns ValueTask):
public sealed class PostgresFixture : IAsyncLifetime
{
public PostgresDatabase Postgres { get; } = new("postgres:18-alpine", Migrate);
public async ValueTask InitializeAsync() => await Postgres.StartAsync();
public ValueTask DisposeAsync() => Postgres.DisposeAsync();
}
xUnit 2 (IAsyncLifetime returns Task):
public sealed class PostgresFixture : IAsyncLifetime
{
public PostgresDatabase Postgres { get; } = new("postgres:18-alpine", Migrate);
public Task InitializeAsync() => Postgres.StartAsync();
public async Task DisposeAsync() => await Postgres.DisposeAsync();
}
Share it as a collection fixture ([CollectionDefinition] with ICollectionFixture<PostgresFixture>) so one container serves every test class, or as a class fixture for one class.
Members:
new PostgresDatabase(image, migrate?): pin the image by tag (postgres:18-alpine,pgvector/pgvector:<tag>), as the service runs it.migrategetsConnectionStringonce, after the server accepts connections.ConnectionString,DataSource: the server's database (anNpgsqlDataSource, disposed with the server).ResetAsync(): onetruncate ... restart identity cascadeover every table of every schema, read from the catalog, so a new migration's table needs no list update. It keeps DbUp's journal (schemaversions) and tables that belong to an extension. Seeded rows a test needs back are yours to insert after.CreateDatabaseAsync(): an empty database with a new name, returned; the callback does not run on it (a test that builds an older schema step by step).DropDatabaseAsync(name)drops it, closing connections still open.ConnectionStringFor(name): the same server, another database, which need not exist (a migrator that creates its database).Unavailable: see below.
Without Docker: outside CI (the CI variable is not true), StartAsync does not throw when no Docker engine answers; it sets Unavailable to the reason, and every member that needs the server throws InvalidOperationException with it. Under xUnit v3 skip on it, e.g. in a fixture method the tests call first:
Assert.SkipWhen(Postgres.Unavailable is not null, Postgres.Unavailable ?? "");
Under xUnit 2 the tests fail with that message. In CI a missing engine fails StartAsync.
Limits:
- One container per
PostgresDatabase, with Testcontainers' default user, password and database. Tests that share it and callResetAsyncmust not run in parallel with each other (one xUnit collection). - Native AOT: not supported and not needed (test code runs on the JIT); the package sets
IsAotCompatibleto false and has no case in the AOT smoke sample.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
-
net10.0
- Npgsql (>= 10.0.3)
- Testcontainers.PostgreSql (>= 4.15.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0 | 44 | 10/14/2026 |