fix: share one PostgreSQL Testcontainer across all test projects

Create Heimdall.TestCommon project with a single PostgreSqlContainerFixture
shared by both Infrastructure and Integration tests. This prevents Docker
resource conflicts from starting two PostgreSQL containers and ensures
both test suites connect to the same database instance.

- Move shared fixture to tests/Heimdall.TestCommon
- Remove duplicate fixtures from each test project
- Keep test isolation (truncation) and FK fix from previous commit
- Revert DATABASE_URL env var check (Testcontainers works in CI)
This commit is contained in:
Arnaud Dartois committed 2026-07-21 18:48:39 +00:00
1 parent 53e0d4c199
commit b167c2afcf
12 files changed
+83 -122

No files matched your search

@@ -9,6 +9,8 @@ using Microsoft.Extensions.Logging;
using NSubstitute;
using Shouldly;
using Heimdall.TestCommon;
namespace Heimdall.Infrastructure.Tests;
[Collection("PostgresInfra")]
@@ -10,6 +10,8 @@ using Microsoft.Extensions.Logging;
using NSubstitute;
using Shouldly;
using Heimdall.TestCommon;
namespace Heimdall.Infrastructure.Tests;
[Collection("PostgresInfra")]
@@ -12,7 +12,6 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="NSubstitute" Version="6.0.0" />
<PackageReference Include="Shouldly" Version="4.3.0" />
<PackageReference Include="Testcontainers.PostgreSql" Version="4.13.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>
@@ -23,6 +22,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Heimdall.Infrastructure\Heimdall.Infrastructure.csproj" />
<ProjectReference Include="..\Heimdall.TestCommon\Heimdall.TestCommon.csproj" />
</ItemGroup>
</Project>
@@ -1,56 +0,0 @@
using Testcontainers.PostgreSql;
namespace Heimdall.Infrastructure.Tests;
/// <summary>
/// xUnit collection fixture that manages a PostgreSQL Testcontainer.
/// One container per test collection — shared across all test classes.
/// In CI mode (DATABASE_URL env var), uses the CI-provided PostgreSQL service
/// instead of starting a Testcontainer.
/// </summary>
public class PostgreSqlContainerFixture : IAsyncLifetime
{
private readonly PostgreSqlContainer? _container;
private readonly string _connectionString;
public string ConnectionString => _connectionString;
public PostgreSqlContainerFixture()
{
var envUrl = Environment.GetEnvironmentVariable("DATABASE_URL");
if (!string.IsNullOrEmpty(envUrl))
{
// CI mode — use the CI-provided PostgreSQL service
_connectionString = envUrl;
_container = null;
}
else
{
// Local dev — start a Testcontainer
_container = new PostgreSqlBuilder("postgres:17-alpine")
.WithDatabase("heimdall_test")
.WithUsername("heimdall")
.WithPassword("heimdall_test_pwd")
.WithCleanUp(true)
.Build();
_connectionString = _container.GetConnectionString();
}
}
public async Task InitializeAsync()
{
if (_container is not null)
await _container.StartAsync();
}
public async Task DisposeAsync()
{
if (_container is not null)
await _container.DisposeAsync();
}
}
[CollectionDefinition("PostgresInfra")]
public class PostgresInfraFixture : ICollectionFixture<PostgreSqlContainerFixture>
{
}
@@ -0,0 +1,12 @@
using Heimdall.TestCommon;
namespace Heimdall.Infrastructure.Tests;
/// <summary>
/// Collection definition for Infrastructure tests that need PostgreSQL.
/// Uses the shared <see cref="PostgreSqlContainerFixture"/> from Heimdall.TestCommon.
/// </summary>
[CollectionDefinition("PostgresInfra")]
public sealed class PostgresInfraDefinition : ICollectionFixture<PostgreSqlContainerFixture>
{
}
@@ -0,0 +1 @@
global using Heimdall.TestCommon;
@@ -0,0 +1,12 @@
using Heimdall.TestCommon;
namespace Heimdall.IntegrationTests.Controllers;
/// <summary>
/// Collection definition for Integration tests that need PostgreSQL.
/// Uses the shared <see cref="PostgreSqlContainerFixture"/> from Heimdall.TestCommon.
/// </summary>
[CollectionDefinition("Postgres")]
public sealed class PostgresDefinition : ICollectionFixture<PostgreSqlContainerFixture>
{
}
@@ -1,6 +1,7 @@
using Heimdall.Infrastructure.Data;
using Heimdall.Infrastructure.Data.Interceptors;
using Heimdall.IntegrationTests.Factories;
using Heimdall.TestCommon;
using Microsoft.EntityFrameworkCore;
namespace Heimdall.IntegrationTests.Controllers;
@@ -8,7 +9,7 @@ namespace Heimdall.IntegrationTests.Controllers;
/// <summary>
/// Base class for integration tests that need a real PostgreSQL database
/// via Testcontainers. All test classes in the "Postgres" collection share
/// the same PostgreSQL container.
/// the same PostgreSQL database.
/// Resets the database before each test to guarantee isolation.
/// </summary>
[Collection("Postgres")]
@@ -1,62 +0,0 @@
using Testcontainers.PostgreSql;
namespace Heimdall.IntegrationTests.Factories;
/// <summary>
/// xUnit collection fixture that manages a PostgreSQL Testcontainer
/// for integration tests. One container per test collection (shared
/// across all test classes in the same collection).
/// In CI mode (DATABASE_URL env var), uses the CI-provided PostgreSQL
/// service instead of starting a Testcontainer.
/// </summary>
public class PostgreSqlContainerFixture : IAsyncLifetime
{
private readonly PostgreSqlContainer? _container;
private readonly string _connectionString;
/// <summary>Connection string for the running PostgreSQL instance.</summary>
public string ConnectionString => _connectionString;
public PostgreSqlContainerFixture()
{
var envUrl = Environment.GetEnvironmentVariable("DATABASE_URL");
if (!string.IsNullOrEmpty(envUrl))
{
// CI mode — use the CI-provided PostgreSQL service
_connectionString = envUrl;
_container = null;
}
else
{
// Local dev — start a Testcontainer
_container = new PostgreSqlBuilder("postgres:17-alpine")
.WithDatabase("heimdall_test")
.WithUsername("heimdall")
.WithPassword("heimdall_test_pwd")
.WithCleanUp(true)
.Build();
_connectionString = _container.GetConnectionString();
}
}
public async Task InitializeAsync()
{
if (_container is not null)
await _container.StartAsync();
}
public async Task DisposeAsync()
{
if (_container is not null)
await _container.DisposeAsync();
}
}
/// <summary>
/// Defines the "Postgres" test collection. All test classes in this
/// collection share the same PostgreSQL container via the fixture.
/// </summary>
[CollectionDefinition("Postgres")]
public class PostgresCollectionFixture : ICollectionFixture<PostgreSqlContainerFixture>
{
}
@@ -10,11 +10,9 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.10" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="NSubstitute" Version="6.0.0" />
<PackageReference Include="Shouldly" Version="4.3.0" />
<PackageReference Include="Testcontainers.PostgreSql" Version="4.13.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>
@@ -26,6 +24,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Heimdall.Web\Heimdall.Web.csproj" />
<ProjectReference Include="..\..\src\Heimdall.Infrastructure\Heimdall.Infrastructure.csproj" />
<ProjectReference Include="..\Heimdall.TestCommon\Heimdall.TestCommon.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Testcontainers.PostgreSql" Version="4.13.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.10" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.0" />
<PackageReference Include="xunit" Version="2.9.3" />
</ItemGroup>
</Project>
@@ -0,0 +1,33 @@
using Testcontainers.PostgreSql;
using Xunit;
namespace Heimdall.TestCommon;
/// <summary>
/// xUnit collection fixture that manages ONE PostgreSQL Testcontainer
/// for ALL database-dependent tests across test projects.
/// </summary>
public sealed class PostgreSqlContainerFixture : IAsyncLifetime
{
private readonly PostgreSqlContainer _container = new PostgreSqlBuilder("postgres:17-alpine")
.WithDatabase("heimdall_test")
.WithUsername("heimdall")
.WithPassword("heimdall_test_pwd")
.WithCleanUp(true)
.Build();
public string ConnectionString => _container.GetConnectionString();
public async Task InitializeAsync()
{
await _container.StartAsync();
}
public async Task DisposeAsync()
{
await _container.DisposeAsync();
}
}
// Collection definitions are in each test project assembly
// (xUnit requires [CollectionDefinition] in the same assembly as the test).