Soenneker.Tests.HostedUnit 11.0.362

Prefix Reserved
dotnet add package Soenneker.Tests.HostedUnit --version 11.0.362
                    
NuGet\Install-Package Soenneker.Tests.HostedUnit -Version 11.0.362
                    
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="11.0.362" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Tests.HostedUnit" Version="11.0.362" />
                    
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 11.0.362
                    
#r "nuget: Soenneker.Tests.HostedUnit, 11.0.362"
                    
#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@11.0.362
                    
#: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=11.0.362
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Tests.HostedUnit&version=11.0.362
                    
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 net11.0 is compatible. 
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
11.0.362 1,440 11/7/2026
11.0.361 1,288 11/7/2026
11.0.360 1,628 11/7/2026
11.0.359 792 11/7/2026
11.0.358 5,901 11/4/2026
11.0.357 346 11/4/2026
11.0.356 1,305 11/4/2026
11.0.355 3,899 11/3/2026
11.0.353 2,938 11/3/2026
11.0.352 1,200 11/3/2026
11.0.351 1,469 11/3/2026
11.0.350 1,227 11/2/2026
11.0.349 553 11/2/2026
11.0.348 6,971 10/29/2026
11.0.347 5,759 10/27/2026
11.0.346 6,104 10/26/2026
11.0.343 1,264 10/26/2026
11.0.342 588 10/26/2026
11.0.341 266 10/26/2026
11.0.340 3,051 10/26/2026
Loading failed