Fusonic.Extensions.UnitTests.EntityFrameworkCore
11.0.0
Prefix Reserved
dotnet add package Fusonic.Extensions.UnitTests.EntityFrameworkCore --version 11.0.0
NuGet\Install-Package Fusonic.Extensions.UnitTests.EntityFrameworkCore -Version 11.0.0
<PackageReference Include="Fusonic.Extensions.UnitTests.EntityFrameworkCore" Version="11.0.0" />
<PackageVersion Include="Fusonic.Extensions.UnitTests.EntityFrameworkCore" Version="11.0.0" />
<PackageReference Include="Fusonic.Extensions.UnitTests.EntityFrameworkCore" />
paket add Fusonic.Extensions.UnitTests.EntityFrameworkCore --version 11.0.0
#r "nuget: Fusonic.Extensions.UnitTests.EntityFrameworkCore, 11.0.0"
#:package Fusonic.Extensions.UnitTests.EntityFrameworkCore@11.0.0
#addin nuget:?package=Fusonic.Extensions.UnitTests.EntityFrameworkCore&version=11.0.0
#tool nuget:?package=Fusonic.Extensions.UnitTests.EntityFrameworkCore&version=11.0.0
Unit tests
This documentation covers the following packages:
| Package | Purpose |
|---|---|
Fusonic.Extensions.UnitTests |
Test base classes with dependency injection (SimpleInjector or Microsoft.Extensions.DependencyInjection) and configuration |
Fusonic.Extensions.UnitTests.EntityFrameworkCore |
Database tests with EF Core: one database per test |
Fusonic.Extensions.UnitTests.EntityFrameworkCore.Npgsql |
PostgreSQL support |
Fusonic.Extensions.UnitTests.EntityFrameworkCore.SqlServer |
Microsoft SQL Server support |
The packages target xUnit v3. Your test project must reference the test framework itself (xunit.v3, a runner such as xunit.runner.visualstudio or Microsoft.Testing.Platform) and the assertion library of your choice. When using SimpleInjector, reference SimpleInjector as well. It is not a transitive dependency of Fusonic.Extensions.UnitTests.
- Unit tests
Introduction
The unit test framework provides the following features:
- Simplicity: The helper methods have short names to reduce clutter, e.g.
Scopedinstead ofRunInSeparateLifetimeScope.- Resolving types:
GetInstance<T>()orGetInstance(Type). - Scope separation: In reality, creating data and consuming that data is not done in the same scope.
ScopedandScopedAsyncrun your code in a separate lifetime scope of the DI container, so you see issues that only occur across scopes. Run data preparation and queries in different scopes.
- Resolving types:
- Dependency injection: SimpleInjector and Microsoft.Extensions.DependencyInjection are supported.
- Database support:
Fusonic.Extensions.UnitTests.EntityFrameworkCoreenables fast, parallel database tests, with providers for PostgreSQL and Microsoft SQL Server. - Configuration support: Microsoft.Extensions.Configuration is supported out of the box with usable default settings.
Setup
Create a TestFixture to register your dependencies and a TestBase that ties the test base class of the extensions to your fixture. All types are in the namespace Fusonic.Extensions.UnitTests.
DependencyInjectionUnitTest<TFixture> implements xUnit's IClassFixture<TFixture>, so every test class gets one fixture instance and therefore one container.
In RegisterCoreDependencies you register the dependencies that all tests need (Mediator, services, ASP.NET Core services and the like). Seal it, so specific fixtures do not override it by accident. Use RegisterDependencies for test specific registrations.
Microsoft.Extensions.DependencyInjection
Inherit from ServiceProviderTestFixture. The service provider is built with ValidateOnBuild and ValidateScopes enabled. Override ServiceProviderOptions to change that.
public class TestFixture : ServiceProviderTestFixture
{
protected sealed override void RegisterCoreDependencies(IServiceCollection services)
{
// Your core dependencies here.
}
}
SimpleInjector
Inherit from SimpleInjectorTestFixture. The container is configured with DefaultLifestyle = Lifestyle.Scoped, DefaultScopedLifestyle = new AsyncScopedLifestyle() and AllowOverridingRegistrations = true, and it is verified after registration. Override VerifyContainer to return false to skip the verification.
public class TestFixture : SimpleInjectorTestFixture
{
protected sealed override void RegisterCoreDependencies(Container container)
{
// Your core dependencies here.
}
}
Test base
The test class requires the fixture as type parameter. Create the following base classes:
public abstract class TestBase<TFixture>(TFixture fixture) : DependencyInjectionUnitTest<TFixture>(fixture)
where TFixture : TestFixture;
public abstract class TestBase(TestFixture fixture) : TestBase<TestFixture>(fixture)
{
// Add common helper methods and properties to this class, e.g.:
/// <summary> Runs a mediator command in its own scope. Used to reduce possible side effects from test data creation and the like. </summary>
[DebuggerStepThrough]
protected Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request)
=> ScopedAsync(() => GetInstance<IMediator>().Send(request));
}
Inherit your test classes from TestBase. If you need specific fixtures, inherit them from TestFixture, override RegisterDependencies and inherit the test class from TestBase<TFixture>. The base implementation of RegisterDependencies is empty.
public class FixtureForASpecificTestClass : TestFixture
{
protected override void RegisterDependencies(IServiceCollection services) // Container container for SimpleInjector
{
// Your test specific registrations here.
}
}
public class SpecificTests(FixtureForASpecificTestClass fixture) : TestBase<FixtureForASpecificTestClass>(fixture)
{
[Fact]
public async Task Test()
{
var result = await ScopedAsync(() => GetInstance<IMyService>().DoAsync());
// ...
}
}
Test settings / Microsoft.Extensions.Configuration
The fixture provides the configuration in its Configuration property (Fixture.Configuration in the test class). That's the default configuration:
builder.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("testsettings.json", optional: true)
.AddUserSecrets(fixtureAssembly, optional: true)
.AddEnvironmentVariables();
The default configuration is built once and shared by all fixtures of the test assembly. To change it, override BuildConfiguration in your TestFixture:
public class TestFixture : ServiceProviderTestFixture
{
protected override IConfiguration BuildConfiguration()
=> new ConfigurationBuilder().AddJsonFile("mysettings.json").Build();
protected sealed override void RegisterCoreDependencies(IServiceCollection services)
{
var settings = Configuration.Get<TestSettings>();
// ...
}
}
Database setup
For database support use Fusonic.Extensions.UnitTests.EntityFrameworkCore (namespace Fusonic.Extensions.UnitTests.EntityFrameworkCore) together with
Fusonic.Extensions.UnitTests.EntityFrameworkCore.Npgsqlfor PostgreSQL orFusonic.Extensions.UnitTests.EntityFrameworkCore.SqlServerfor Microsoft SQL Server.
Every test gets its own copy of a template database. This enables parallel database tests and keeps tests from affecting each other. The setup consists of three parts:
- A test startup that starts the database server and creates the template once per test run.
- An
ITestStoreregistered as singleton, together with yourDbContext, in the fixture. - A test base inheriting from
DatabaseUnitTest.
Database test base
DatabaseUnitTest<TDbContext, TFixture> uses TDbContext as default DbContext. Use DatabaseUnitTest<TFixture> if you do not want a default DbContext. Example base classes:
public abstract class TestBase(TestFixture fixture) : TestBase<TestFixture>(fixture);
public abstract class TestBase<TFixture>(TFixture fixture) : DatabaseUnitTest<AppDbContext, TFixture>(fixture)
where TFixture : TestFixture;
DatabaseUnitTest provides the methods Query and QueryAsync. They resolve the DbContext in a separate scope and run your query. So instead of writing
var count = await ScopedAsync(() =>
{
var context = GetInstance<AppDbContext>();
return context.Documents.CountAsync();
});
you can just write
var count = await QueryAsync(ctx => ctx.Documents.CountAsync());
For other DbContext types, specify the type: QueryAsync<OtherDbContext>(ctx => ...) and Query<OtherDbContext>(ctx => ...) for queries without a result, QueryAsync<OtherDbContext, int>(ctx => ...) and Query<OtherDbContext, int>(ctx => ...) for queries with a result.
DatabaseUnitTest implements IAsyncLifetime. The test database is dropped in DisposeAsync. When you override InitializeAsync or DisposeAsync, call the base implementation.
Test startup
Start the database server and create the template once per test run with an xUnit v3 assembly fixture. TestContainers is a simple way to get the database server running locally. In CI the database can be provided as service, passed in by an environment variable. Example for PostgreSQL:
[assembly: AssemblyFixture(typeof(TestStartup))]
public class TestStartup : IAsyncLifetime
{
public static string ConnectionString { get; private set; } = null!;
public async ValueTask InitializeAsync()
{
// In CI the database is provided as a service. Locally, a test container is started.
var connectionString = Environment.GetEnvironmentVariable("ConnectionStrings__Postgres");
if (string.IsNullOrWhiteSpace(connectionString))
{
var container = new PostgreSqlBuilder("postgres:18-alpine")
.WithReuse(true)
.WithName("test.myproject")
.WithTmpfsMount("/var/lib/postgresql") // Keep the database in memory, which speeds up the tests significantly.
.WithTmpfsMount("/dev/shm")
.Build();
await container.StartAsync();
connectionString = container.GetConnectionString();
}
// The database name is the name of the template.
ConnectionString = new NpgsqlConnectionStringBuilder(connectionString) { Database = "myproject_test" }.ConnectionString;
await PostgreSqlUtil.CreateTestDbTemplate<AppDbContext>(
ConnectionString,
o => new AppDbContext(o),
seed: ctx => new TestDataSeed(ctx).Seed());
}
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
}
For SQL Server, use MsSqlBuilder from Testcontainers.MsSql, set InitialCatalog instead of Database and call SqlServerTestUtil.CreateTestDbTemplate.
CreateTestDbTemplate (both PostgreSqlUtil and SqlServerTestUtil) has these optional parameters:
npgsqlOptionsAction/sqlServerOptionsAction: configures the provider, e.g. forUseNetTopologySuite().seed: seeds the template after creation.logger: defaults to a console logger.useMigrations: applies the migrations (default). Set it tofalseto create the schema withEnsureCreated. Since EF Core 11, applying migrations throws if theDbContexthas no migrations, so set it tofalsefor contexts without migrations.overwrite: by default the template is only created if it does not exist yet, which speeds up local test runs. Set it totrueto always create a fresh template. Withoutoverwrite, delete the template (or the reused container) after changing the schema.
PostgreSqlUtil and SqlServerTestUtil also provide CheckDatabaseExists and DropDatabase.
PostgreSQL
Register the NpgsqlDatabasePerTestStore with the template connection string. The database name in the connection string is the template name and must not be postgres. The test database is created lazily when the first connection is opened.
public class TestFixture : ServiceProviderTestFixture
{
protected sealed override void RegisterCoreDependencies(IServiceCollection services)
{
var testStore = new NpgsqlDatabasePerTestStore(TestStartup.ConnectionString);
services.AddSingleton<ITestStore>(testStore);
services.AddDbContext<AppDbContext>(b => b.UseNpgsqlDatabasePerTest(testStore));
}
}
UseNpgsqlDatabasePerTest has an optional npgsqlOptionsAction parameter to configure the provider.
When using IDbContextFactory, the factory must be registered with scoped lifetime, not with the default singleton lifetime:
services.AddDbContextFactory<AppDbContext>(b => b.UseNpgsqlDatabasePerTest(testStore), ServiceLifetime.Scoped);
Microsoft SQL Server
Register the SqlServerDatabasePerTestStore with the template connection string. The Initial Catalog of the connection string is the template name and must not be master.
public class TestFixture : ServiceProviderTestFixture
{
protected sealed override void RegisterCoreDependencies(IServiceCollection services)
{
var testStore = new SqlServerDatabasePerTestStore(TestStartup.ConnectionString);
services.AddSingleton<ITestStore>(testStore);
services.AddDbContext<AppDbContext>(b => b.UseSqlServerDatabasePerTest(testStore));
}
}
SQL Server creates a backup of the template (<template>.bak) and restores it for each test. The files are stored in the data directory on the SQL Server host. It defaults to /var/opt/mssql/data, which matches the official Linux Docker image. Pass dataDirectoryPath to the constructor if your server uses another directory. The optional databasePrefix sets a prefix for the test database names.
UseSqlServerDatabasePerTest has an optional sqlServerOptionsAction parameter to configure the provider.
When using IDbContextFactory, the factory must be registered with scoped lifetime, not with the default singleton lifetime:
services.AddDbContextFactory<AppDbContext>(b => b.UseSqlServerDatabasePerTest(testStore), ServiceLifetime.Scoped);
Database tests with SimpleInjector
AddDbContext is only available for IServiceCollection, so register the DbContext in a ServiceCollection and cross-wire it into SimpleInjector (requires SimpleInjector.Integration.ServiceCollection). DatabaseUnitTest resolves the ITestStore from the container, so it must be resolvable there as singleton.
public class TestFixture : SimpleInjectorTestFixture
{
protected sealed override void RegisterCoreDependencies(Container container)
{
var services = new ServiceCollection();
var testStore = new NpgsqlDatabasePerTestStore(TestStartup.ConnectionString);
services.AddSingleton<ITestStore>(testStore);
services.AddDbContext<AppDbContext>(b => b.UseNpgsqlDatabasePerTest(testStore));
services.AddSimpleInjector(container, setup => setup.AutoCrossWireFrameworkComponents = true);
services.BuildServiceProvider(validateScopes: true).UseSimpleInjector(container);
}
}
Configuring any other database
The database support is not limited to PostgreSQL and SQL Server. Implement and register an ITestStore:
OnTestConstruction()is called in the constructor of eachDatabaseUnitTest. Create a new database name for the test here.OnTestEnd()is called inDisposeAsyncof the test. Drop the test database here.
To create the database lazily when the first connection opens, add new ConnectionOpeningInterceptor(testStore.CreateDatabase) to the DbContextOptionsBuilder with AddInterceptors. For an example with SQLite, see SqliteTestStore and its TestFixture.
Support multiple databases in a test
You can test with multiple database systems at once. Instead of registering the test stores one by one, combine them in an AggregateTestStore:
public class TestFixture : ServiceProviderTestFixture
{
protected sealed override void RegisterCoreDependencies(IServiceCollection services)
{
var npgsqlTestStore = new NpgsqlDatabasePerTestStore(TestStartup.NpgsqlConnectionString);
services.AddDbContext<NpgsqlDbContext>(b => b.UseNpgsqlDatabasePerTest(npgsqlTestStore));
var sqlServerTestStore = new SqlServerDatabasePerTestStore(TestStartup.SqlServerConnectionString);
services.AddDbContext<SqlServerDbContext>(b => b.UseSqlServerDatabasePerTest(sqlServerTestStore));
services.AddSingleton<ITestStore>(new AggregateTestStore(npgsqlTestStore, sqlServerTestStore));
}
}
Running in GitLab
There are two options to provide the database in GitLab CI.
Database as service: Start the database as a service and pass the connection string as environment variable. The test startup above uses it instead of starting a container:
dotnet:test:
services:
- name: postgres:18-alpine
alias: postgres
variables:
POSTGRES_PASSWORD: postgres
ConnectionStrings__Postgres: "Host=postgres;Username=postgres;Password=postgres"
TestContainers in Docker-in-Docker: Start docker:dind-rootless as a service. When running rootless dind, you also must set TESTCONTAINERS_RYUK_DISABLED to true, as there is no docker.sock available. Ryuk is responsible for cleaning up the test containers, even when test jobs get cancelled. Disabling it is safe, as the containers started within docker:dind get cleaned up after the job ends anyway.
variables:
DOCKER_VERSION: "29"
DOCKER_BUILDKIT: 1
DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_CERTDIR: "/certs/${CI_JOB_ID}"
DOCKER_TLS_VERIFY: 1
DOCKER_CERT_PATH: "/certs/${CI_JOB_ID}/client"
TESTCONTAINERS_RYUK_DISABLED: "true" # Disable Ryuk as we're running in dind-rootless and there is no docker.sock available.
dotnet:test:
services:
- docker:${DOCKER_VERSION}-dind-rootless
script:
- echo "Running Tests..."
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Fusonic.Extensions.UnitTests (>= 11.0.0)
- Microsoft.EntityFrameworkCore (>= 11.0.0)
- Microsoft.EntityFrameworkCore.Relational (>= 11.0.0)
- Microsoft.Extensions.Configuration (>= 11.0.0)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 11.0.0)
- Microsoft.Extensions.Configuration.FileExtensions (>= 11.0.0)
- Microsoft.Extensions.Configuration.Json (>= 11.0.0)
- Microsoft.Extensions.Configuration.UserSecrets (>= 11.0.0)
- Microsoft.Extensions.DependencyInjection (>= 11.0.0)
- Polly (>= 8.9.1)
- xunit.v3.extensibility.core (>= 4.0.3)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Fusonic.Extensions.UnitTests.EntityFrameworkCore:
| Package | Downloads |
|---|---|
|
Fusonic.Extensions.UnitTests.EntityFrameworkCore.Npgsql
Adds support for database tests using EF Core with PostgreSQL. |
|
|
Fusonic.Extensions.UnitTests.EntityFrameworkCore.SqlServer
Adds support for database tests using EF Core with SqlServer. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 11.0.0 | 0 | 11/23/2026 |
| 10.3.0 | 161 | 11/9/2026 |
| 10.2.0 | 121 | 11/5/2026 |
| 10.1.0 | 3,615 | 3/18/2026 |
| 10.0.1 | 185 | 3/17/2026 |
| 10.0.0 | 642 | 11/25/2025 |
| 10.0.0-rc.2 | 210 | 11/24/2025 |
| 10.0.0-rc.1 | 172 | 9/12/2025 |
| 9.5.4 | 398 | 8/20/2025 |
| 9.5.3 | 342 | 3/28/2025 |
| 9.5.2 | 304 | 1/16/2025 |
| 9.5.0 | 344 | 10/4/2024 |
| 9.4.0 | 300 | 9/23/2024 |
| 9.3.1 | 357 | 9/11/2024 |
| 9.3.0 | 318 | 9/11/2024 |
| 9.2.0 | 319 | 8/8/2024 |
| 9.2.0-rc.3 | 148 | 8/7/2024 |
| 9.2.0-rc.2 | 129 | 8/5/2024 |
| 9.2.0-rc.1 | 145 | 8/1/2024 |
| 9.1.0 | 273 | 7/29/2024 |