Skip to main content
.NETFramework4.6.2net8.0net9.0

This package creates MSTest test classes from an application-owned Microsoft.Extensions.Hosting service provider with one dependency-injection scope per test invocation. MSTest is Microsoft supported Test Framework.

Published
Oct 27, 2026
6d ago
Total Downloads
206
206

Install

dotnet add package MSTest.Extensions.Hosting --version 4.5.2-alpha.26527.6
ProductVersions
.NET
net8.0net9.0
Other
.NETFramework4.6.2

README

MSTest.Extensions.Hosting

MSTest.Extensions.Hosting lets an application-owned Microsoft.Extensions.Hosting container create MSTest test classes.

This package remains prerelease, including in MSTest RTM builds, while its Microsoft.Testing.Extensions.Hosting dependency is alpha.

Register the integration while building the same host that runs Microsoft Testing Platform:

HostApplicationBuilder builder = Host.CreateApplicationBuilder();
builder.Services.AddSingleton<MyApplicationService>();
builder.Services.AddMSTestTestClassInjection();

Test classes can then request registered services in a public constructor:

[TestClass]
public sealed class MyTests(MyApplicationService service, TestContext testContext)
{
    [TestMethod]
    public void UsesApplicationService()
        => Assert.IsNotNull(service);
}

The integration is explicit and affects only MSTest test-class construction. It creates one IServiceScope for every test invocation, including each data row and retry attempt. TestInitialize, the test method, and TestCleanup share the same test-class instance and scope. Static ClassInitialize and ClassCleanup run outside invocation scopes.

The application host remains caller-owned. MSTest never disposes the host or its root service provider. After TestCleanup, the integration disposes the test-class instance and then its invocation scope.

NativeAOT, browser WebAssembly, AOT compilation, and MSTest source generation are not supported by this first reflection-based integration. Calling AddMSTestTestClassInjection in one of those modes produces a deterministic build error, including when host registration is compiled into a referenced project.

Changes from Previous Version

No structural changes between v4.5.1-alpha.26507.8 and v4.5.2-alpha.26527.6.

Adoption Assessment

A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.

This version is marked prerelease. Validate its API and upgrade path before production use.Compatibility metadata lists 3 target frameworks; confirm the exact target used by your application.The published metadata declares 15 direct dependency entries across framework groups. Inspect the resolved dependency graph for your target framework.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.