Soenneker.Tests.HostedUnit 4.0.218

Prefix Reserved
There is a newer version of this package available.
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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Soenneker.Tests.HostedUnit" Version="4.0.218" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Tests.HostedUnit" Version="4.0.218" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Tests.HostedUnit" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Soenneker.Tests.HostedUnit --version 4.0.218
                    
#r "nuget: Soenneker.Tests.HostedUnit, 4.0.218"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Soenneker.Tests.HostedUnit@4.0.218
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Soenneker.Tests.HostedUnit&version=4.0.218
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Tests.HostedUnit&version=4.0.218
                    
Install as a Cake Tool

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
Loading failed