Soenneker.TestHosts.Unit
11.0.233
Prefix Reserved
dotnet add package Soenneker.TestHosts.Unit --version 11.0.233
NuGet\Install-Package Soenneker.TestHosts.Unit -Version 11.0.233
<PackageReference Include="Soenneker.TestHosts.Unit" Version="11.0.233" />
<PackageVersion Include="Soenneker.TestHosts.Unit" Version="11.0.233" />
<PackageReference Include="Soenneker.TestHosts.Unit" />
paket add Soenneker.TestHosts.Unit --version 11.0.233
#r "nuget: Soenneker.TestHosts.Unit, 11.0.233"
#:package Soenneker.TestHosts.Unit@11.0.233
#addin nuget:?package=Soenneker.TestHosts.Unit&version=11.0.233
#tool nuget:?package=Soenneker.TestHosts.Unit&version=11.0.233
Soenneker.TestHosts.Unit
A TUnit-compatible shared test host with dependency injection, Serilog-to-test-output logging, Bogus, and AutoFaker.
Installation
dotnet add package Soenneker.TestHosts.Unit
Define a host
using Microsoft.Extensions.DependencyInjection;
using Soenneker.TestHosts.Unit;
public sealed class Host : UnitTestHost
{
public override Task InitializeAsync()
{
Services.AddSingleton<IClock, TestClock>();
Services.AddScoped<OrderService>();
return base.InitializeAsync();
}
}
Add registrations before calling base.InitializeAsync(). Initialization builds one service provider with scope validation enabled. Changes made to Services after that point do not alter the built provider.
Share it with TUnit
using Microsoft.Extensions.DependencyInjection;
[ClassDataSource<Host>(Shared = SharedType.PerTestSession)]
public sealed class OrderServiceTests
{
private readonly Host _host;
public OrderServiceTests(Host host)
{
_host = host;
}
[Test]
public async Task Creates_an_order()
{
await using AsyncServiceScope scope = _host.ServicesProvider.CreateAsyncScope();
var service = scope.ServiceProvider.GetRequiredService<OrderService>();
var command = _host.AutoFaker.Generate<CreateOrder>();
Order result = await service.Create(command);
await Assert.That(result.Id).IsNotEqualTo(Guid.Empty);
}
}
Create a scope when resolving scoped services; resolving one directly from the root provider fails because scope validation is enabled. ServicesProvider is available only after initialization.
Lifecycle
TUnit calls the host's asynchronous initialization and disposal through its data-source lifecycle. Disposing the host disposes the service provider and therefore any disposable services owned by it, then releases the logging pipeline. Test output written through ILogger<T> or Serilog is routed to the active TUnit context.
Faker and AutoFaker are created lazily and shared with the host. A per-test-session host therefore shares their randomizer state across its tests; do not rely on call order when tests execute concurrently.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Serilog.Extensions.Logging (>= 10.0.0)
- Soenneker.Asyncs.Initializers (>= 11.0.140)
- Soenneker.Serilog.Sinks.TUnit (>= 11.0.172)
- Soenneker.Utils.AutoBogus (>= 11.0.1011)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Soenneker.TestHosts.Unit:
| Package | Downloads |
|---|---|
|
Soenneker.Tests.HostedUnit
A fundamental test that stores the TestHost and provides synthetic inversion of control |
|
|
Soenneker.Playwrights.TestHosts
A test host for automated testing with Playwright |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 11.0.233 | 0 | 11/15/2026 |
| 11.0.232 | 2,171 | 11/13/2026 |
| 11.0.231 | 3,170 | 11/11/2026 |
| 11.0.229 | 54 | 11/11/2026 |
| 11.0.228 | 967 | 11/11/2026 |
| 11.0.226 | 1,594 | 11/11/2026 |
| 11.0.225 | 2,723 | 11/11/2026 |
| 11.0.224 | 51 | 11/11/2026 |
| 11.0.223 | 16,895 | 11/7/2026 |
| 11.0.222 | 2,947 | 11/7/2026 |
| 11.0.221 | 6,863 | 11/4/2026 |
| 11.0.220 | 1,678 | 11/3/2026 |
| 11.0.219 | 3,995 | 11/3/2026 |
| 11.0.218 | 2,970 | 11/3/2026 |
| 11.0.217 | 1,238 | 11/3/2026 |
| 11.0.216 | 3,159 | 11/2/2026 |
| 11.0.215 | 6,997 | 10/29/2026 |
| 11.0.214 | 5,797 | 10/27/2026 |
| 11.0.213 | 6,158 | 10/26/2026 |
| 11.0.212 | 105 | 10/26/2026 |
Update dependency Soenneker.Serilog.Sinks.TUnit to 11.0.172 (#210)