Soenneker.Tests.HostedUnit
4.0.218
Prefix Reserved
See the version list below for details.
dotnet add package Soenneker.Tests.HostedUnit --version 4.0.218
NuGet\Install-Package Soenneker.Tests.HostedUnit -Version 4.0.218
<PackageReference Include="Soenneker.Tests.HostedUnit" Version="4.0.218" />
<PackageVersion Include="Soenneker.Tests.HostedUnit" Version="4.0.218" />
<PackageReference Include="Soenneker.Tests.HostedUnit" />
paket add Soenneker.Tests.HostedUnit --version 4.0.218
#r "nuget: Soenneker.Tests.HostedUnit, 4.0.218"
#:package Soenneker.Tests.HostedUnit@4.0.218
#addin nuget:?package=Soenneker.Tests.HostedUnit&version=4.0.218
#tool nuget:?package=Soenneker.Tests.HostedUnit&version=4.0.218
Soenneker.Tests.HostedUnit
A TUnit base class for tests that resolve services from a shared UnitTestHost while owning a per-test dependency-injection scope.
Installation
dotnet add package Soenneker.Tests.HostedUnit
Define the 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();
}
}
Define the tests
using Soenneker.Tests.HostedUnit;
[ClassDataSource<Host>(Shared = SharedType.PerTestSession)]
public sealed class OrderServiceTests : HostedUnitTest
{
public OrderServiceTests(Host host) : base(host)
{
}
[Test]
public async Task Creates_an_order()
{
OrderService service = Resolve<OrderService>(scoped: true);
CreateOrder request = AutoFaker.Generate<CreateOrder>();
Order result = await service.Create(request);
await Assert.That(result.Id).IsNotEqualTo(Guid.Empty);
}
}
Resolve<T>() resolves from the shared host's root provider. Resolve<T>(scoped: true) lazily creates one async scope for the test instance, reuses it for subsequent scoped resolutions, and disposes it after the test. CreateScope() can establish that scope explicitly and is idempotent while it exists.
The base class reuses the host's AutoFaker. Its Logger is resolved through the test scope, so log output follows the logging services configured by the host.
Background work
If the host registers IBackgroundQueue, WaitOnQueueToEmpty(cancellationToken) waits for its queued work to finish before assertions or teardown continue. Supply a bounded cancellation token so a stuck producer cannot hang the test run. Accessing this method without an IBackgroundQueue registration fails service resolution.
The ClassDataSource should be shared so TUnit initializes and disposes the host once while each test instance still owns and disposes its own scope.
| 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
- Soenneker.Extensions.ServiceProvider (>= 4.0.333)
- Soenneker.TestHosts.Unit (>= 4.0.128)
- Soenneker.Tests.Unit (>= 4.0.1288)
- Soenneker.Utils.BackgroundQueue (>= 4.0.2475)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Soenneker.Tests.HostedUnit:
| Package | Downloads |
|---|---|
|
Soenneker.Playwrights.Tests.Unit
A fixtured unit test for testing with Playwright |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.0.219 | 0 | 8/31/2026 |
| 4.0.218 | 0 | 8/31/2026 |
| 4.0.217 | 0 | 8/31/2026 |
| 4.0.215 | 0 | 8/31/2026 |
| 4.0.214 | 0 | 8/31/2026 |
| 4.0.213 | 3,379 | 8/30/2026 |
| 4.0.212 | 9,868 | 8/30/2026 |
| 4.0.211 | 3,484 | 8/30/2026 |
| 4.0.210 | 1,679 | 8/29/2026 |
| 4.0.209 | 776 | 8/29/2026 |
| 4.0.207 | 16,498 | 8/27/2026 |
| 4.0.206 | 165 | 8/27/2026 |
| 4.0.205 | 1,353 | 8/27/2026 |
| 4.0.204 | 2,713 | 8/26/2026 |
| 4.0.203 | 720 | 8/26/2026 |
| 4.0.202 | 1,165 | 8/26/2026 |
| 4.0.201 | 689 | 8/26/2026 |
| 4.0.200 | 472 | 8/26/2026 |
| 4.0.199 | 1,029 | 8/25/2026 |
| 4.0.198 | 5,104 | 8/22/2026 |