Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions docs/RFCs/013-Microsoft-Extensions-Bridges.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,59 @@ host owns its service provider, configuration, logging, OpenTelemetry providers,
helper imports only the supported configuration snapshot and logging factory, starts the host before
building MTP, returns MTP's exit code, and stops the host in a `finally` block.

MTP's MSBuild package can generate this composition when `TestingPlatformHostFactory` names a
parameterless static `Task<IHost>` factory. Generated code owns and disposes the fresh unstarted host
(preferring `IAsyncDisposable`),
while the bridge remains responsible for start/stop ordering. The factory does not receive MTP
arguments. C#, Visual Basic, and F# use the same contract, and the existing standalone generated
source remains unchanged when the property is unset.

Direct and response-file `--help`, `-?`, and `--info` options are parsed before invoking the factory.
Configuration-only informational options are resolved later, after the factory-created host supplies
its configuration snapshot; factories therefore must not start services or perform application work
during construction.

Because the same host factory is used for console, JSON listing, server, and `dotnet test` modes, the
host contract requires stdout-silent logging, hosted services, and exporters. Stdout remains reserved
for MTP protocol payloads; hosted applications should use OTLP, files, or another non-stdout sink.

The current generated contract is process-local rather than process-role-aware. Extensions that
restart the test process can invoke the factory in both the controller and child. Deferring factory
creation until MTP selects the final process role requires a future pre-build lifecycle seam; until
then, factories combined with restart extensions must avoid exclusive global resources.

`TestingPlatformOpenTelemetryMode=HostOwned` causes the OpenTelemetry extension package to contribute
one normal `TestingPlatformBuilderHook` that calls only `AddTestingPlatformDiagnostics()`. The host
continues to own the only providers, exporters, resource identity, and dependency-injection container.
The focused test/CI resource helpers remain the recommended composition.

`TestApplicationOptions.CancellationToken` is the compatible core execution contract: it adds one
property to the existing options class instead of adding a source-breaking member to
`ITestApplication`. The token is registered only for the built application's execution lifetime and
the registration is disposed when `RunAsync` completes or the application is disposed.

Host stopping is connected to that contract through a bounded internal,
zero-Microsoft.Extensions lifetime bridge. `RunTestingPlatformAsync` supplies its caller token through
`TestApplicationOptions`, `IHostApplicationLifetime.ApplicationStopping` requests the same MTP
cooperative cancellation, and the helper stops the host after MTP cleanup completes. MTP cancellation
does not call `StopApplication`. The bridge disconnects before the helper's final `StopAsync` so that
normal host shutdown cannot re-enter MTP cancellation after the run has ended.

Caller cancellation, `ApplicationStopping`, Ctrl+C, `--timeout`, controller cancellation, and
test-framework stop policies converge on the existing application cancellation source. They have no
priority ordering: the first signal starts the idempotent shutdown path, cleanup is preserved, and a
canceled active run returns `TestSessionAborted` (`3`). Cancellation during the host's initial
`StartAsync` remains governed by the host contract and can throw `OperationCanceledException`.

The same application token already drives controller and orchestrator cancellation. In controller
mode it requests cooperative child cancellation over the existing control channel, with the existing
bounded termination fallback for a non-cooperative child. No `Microsoft.Extensions.*` dependency is
added to `Microsoft.Testing.Platform`.

Run, build, or framework failures remain primary. The helper disconnects cancellation callbacks before
its uncancelled host shutdown attempt, and a shutdown failure is attached as secondary exception data
(or aggregated when exception data is unavailable) instead of replacing the earlier failure.

It intentionally does not expose MTP as an `IHostedService`, build a second Microsoft.Extensions
container, dispose the caller's host, or imply that live services cross process boundaries. The API
remains experimental while cancellation and out-of-process behavior are evaluated.
Expand All @@ -181,8 +234,6 @@ composition pattern with a generic host, ASP.NET Core, and Aspire ServiceDefault
- **Dependency injection interop** — import only proven, externally owned service instances. Do not
convert the MTP registry into `IServiceCollection`, implicitly build a second container, or transfer
disposal ownership.
- **Hosting cancellation** — consider a cancellable MTP run API before claiming that
`IHostApplicationLifetime.ApplicationStopping` can cancel an active test run.
- **Command-line tooling** — the existing machine-readable `dotnet test` option message includes
provider identity and minimum/maximum arity in addition to name, description, visibility, and
built-in status. This provides tooling metadata without a runtime `System.CommandLine` bridge.
4 changes: 2 additions & 2 deletions docs/glossary.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ An experimental MTP extension (`Microsoft.Testing.Extensions.Configuration`, `[T

### Microsoft.Testing.Extensions.Hosting

An experimental MTP extension (`Microsoft.Testing.Extensions.Hosting`, `[TPEXP]`) that runs MTP inside an application-owned `IHost` through `RunTestingPlatformAsync()`. It borrows the host's configuration and logger factory, starts the host before MTP is built, returns the MTP exit code, and stops the host afterward without disposing it or creating a second Microsoft.Extensions dependency-injection container. See the [ASP.NET Core and Aspire samples](../samples/public/MTPHostIntegration).
An experimental MTP extension (`Microsoft.Testing.Extensions.Hosting`, `[TPEXP]`) that runs MTP inside an application-owned `IHost` through `RunTestingPlatformAsync()`. It borrows the host's configuration and logger factory, starts the host before MTP is built, links both the caller cancellation token and host stopping to MTP cooperative cancellation, returns the MTP exit code, and stops the host afterward without disposing it or creating a second Microsoft.Extensions dependency-injection container. Set `<TestingPlatformHostFactory>Contoso.Tests.TestHost.CreateHost</TestingPlatformHostFactory>` to generate the C#, Visual Basic, or F# composition; the parameterless factory returns `Task<IHost>`, receives no MTP arguments, and transfers disposal ownership to generated code. See the [ASP.NET Core and Aspire samples](../samples/public/MTPHostIntegration).

### Microsoft.Testing.Extensions.Logging

Expand All @@ -428,7 +428,7 @@ A component in MTP that coordinates multi-process test execution. The orchestrat

### OpenTelemetry extension

An MTP extension (`Microsoft.Testing.Extensions.OpenTelemetry`) that exposes test-session activities and metrics using the [OpenTelemetry](https://opentelemetry.io/) standard. `AddTestingPlatformDiagnostics()` activates the MTP diagnostics producer; application-owned providers configured through `HostApplicationBuilder`, Aspire ServiceDefaults, or another composition root subscribe with `AddTestingPlatformInstrumentation()`. The focused `AddTestingPlatformTestResource()` and `AddTestingPlatformCIResource()` helpers add test and CI metadata without replacing application-owned `service.*`, host, OS, or process identity. See the [`HostApplicationBuilder` sample](../samples/public/MTPOTel).
An MTP extension (`Microsoft.Testing.Extensions.OpenTelemetry`) that exposes test-session activities and metrics using the [OpenTelemetry](https://opentelemetry.io/) standard. `AddTestingPlatformDiagnostics()` activates the MTP diagnostics producer; application-owned providers configured through `HostApplicationBuilder`, Aspire ServiceDefaults, or another composition root subscribe with `AddTestingPlatformInstrumentation()`. For generated hosting, `<TestingPlatformOpenTelemetryMode>HostOwned</TestingPlatformOpenTelemetryMode>` contributes a builder hook that calls only `AddTestingPlatformDiagnostics()` and never creates a second provider, exporter, resource identity, or container. The focused `AddTestingPlatformTestResource()` and `AddTestingPlatformCIResource()` helpers add test and CI metadata without replacing application-owned `service.*`, host, OS, or process identity. See the [`HostApplicationBuilder` sample](../samples/public/MTPOTel).

### OSConditionAttribute

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project>

<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk.Web" />

<PropertyGroup>
<EnableMSTestRunner>true</EnableMSTestRunner>
<GenerateTestingPlatformEntryPoint>false</GenerateTestingPlatformEntryPoint>
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
<TestingPlatformHostFactory>AspNetCoreTestHost.CreateHost</TestingPlatformHostFactory>
<TestingPlatformOpenTelemetryMode>HostOwned</TestingPlatformOpenTelemetryMode>
<MicrosoftTestingPlatformMSBuildTaskFolder>$([MSBuild]::NormalizePath($(MSBuildProjectDirectory), '..\..\..\..\artifacts\bin\Microsoft.Testing.Platform.MSBuild\$(Configuration)\netstandard2.0'))\</MicrosoftTestingPlatformMSBuildTaskFolder>
Comment thread
Evangelink marked this conversation as resolved.
Resolved
<NoWarn>$(NoWarn);TPEXP</NoWarn>
</PropertyGroup>

Expand All @@ -14,10 +18,24 @@
<ItemGroup>
<ProjectReference Include="..\ServiceDefaults\ServiceDefaults.csproj" />
<ProjectReference Include="..\..\..\..\src\Platform\Microsoft.Testing.Extensions.Hosting\Microsoft.Testing.Extensions.Hosting.csproj" />
<ProjectReference Include="..\..\..\..\src\Platform\Microsoft.Testing.Extensions.OpenTelemetry\Microsoft.Testing.Extensions.OpenTelemetry.csproj" />
<ProjectReference Include="..\..\..\..\src\Platform\Microsoft.Testing.Platform.MSBuild\Microsoft.Testing.Platform.MSBuild.csproj"
Condition=" '$(BuildingSolutionFile)' != 'true' "
ReferenceOutputAssembly="false"
PrivateAssets="all"
SetConfiguration="Configuration=$(Configuration)"
SetTargetFramework="TargetFramework=netstandard2.0" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>

<Import Project="..\..\..\..\src\Platform\Microsoft.Testing.Extensions.Hosting\buildMultiTargeting\Microsoft.Testing.Extensions.Hosting.props" />
<Import Project="..\..\..\..\src\Platform\Microsoft.Testing.Extensions.OpenTelemetry\buildMultiTargeting\Microsoft.Testing.Extensions.OpenTelemetry.props" />
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.Web" />
<Import Project="..\..\..\..\src\Platform\Microsoft.Testing.Extensions.Hosting\buildMultiTargeting\Microsoft.Testing.Extensions.Hosting.targets" />
<Import Project="..\..\..\..\src\Platform\Microsoft.Testing.Extensions.OpenTelemetry\buildMultiTargeting\Microsoft.Testing.Extensions.OpenTelemetry.targets" />
<Import Project="..\..\..\..\src\Platform\Microsoft.Testing.Platform.MSBuild\buildMultiTargeting\Microsoft.Testing.Platform.MSBuild.targets" />

</Project>
34 changes: 17 additions & 17 deletions samples/public/MTPHostIntegration/AspNetCoreTests/Program.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Reflection;

using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Testing.Extensions;

WebApplicationBuilder builder = WebApplication.CreateBuilder();
builder.AddTestServiceDefaults();
builder.WebHost.UseUrls("http://127.0.0.1:0");
builder.Configuration["Greeting"] = "Hello from ASP.NET Core";

await using WebApplication app = builder.Build();
app.MapDefaultEndpoints();
app.MapGet("/greeting", (IConfiguration configuration) => configuration["Greeting"]);
AspNetCoreApplication.Initialize(app);
using Microsoft.Extensions.Logging;

return await app.RunTestingPlatformAsync(args, tests =>
internal static class AspNetCoreTestHost
{
tests.AddMSTest(() => [Assembly.GetExecutingAssembly()]);
tests.AddTestingPlatformDiagnostics();
});
public static Task<IHost> CreateHost()
{
WebApplicationBuilder builder = WebApplication.CreateBuilder();
builder.Logging.ClearProviders();
builder.AddTestServiceDefaults();
builder.WebHost.UseUrls("http://127.0.0.1:0");
builder.Configuration["Greeting"] = "Hello from ASP.NET Core";

WebApplication app = builder.Build();
app.MapDefaultEndpoints();
app.MapGet("/greeting", (IConfiguration configuration) => configuration["Greeting"]);
AspNetCoreApplication.Initialize(app);
return Task.FromResult<IHost>(app);
}
}

internal static class AspNetCoreApplication
{
Expand Down
22 changes: 20 additions & 2 deletions samples/public/MTPHostIntegration/AspireTests/AspireTests.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<Project Sdk="MSTest.Sdk">
<Project>

<Import Project="Sdk.props" Sdk="MSTest.Sdk" />

<PropertyGroup>
<EnableAspireTesting>true</EnableAspireTesting>
<AspireHostingTestingVersion>$(AspireVersion)</AspireHostingTestingVersion>
<GenerateTestingPlatformEntryPoint>false</GenerateTestingPlatformEntryPoint>
<TestingPlatformHostFactory>AspireTestHost.CreateHost</TestingPlatformHostFactory>
<TestingPlatformOpenTelemetryMode>HostOwned</TestingPlatformOpenTelemetryMode>
<MicrosoftTestingPlatformMSBuildTaskFolder>$([MSBuild]::NormalizePath($(MSBuildProjectDirectory), '..\..\..\..\artifacts\bin\Microsoft.Testing.Platform.MSBuild\$(Configuration)\netstandard2.0'))\</MicrosoftTestingPlatformMSBuildTaskFolder>
Comment thread
Evangelink marked this conversation as resolved.
Resolved
<NoWarn>$(NoWarn);TPEXP</NoWarn>
</PropertyGroup>

Expand All @@ -15,6 +19,20 @@
<ProjectReference Include="..\AspireAppHost\AspireAppHost.csproj" />
<ProjectReference Include="..\ServiceDefaults\ServiceDefaults.csproj" />
<ProjectReference Include="..\..\..\..\src\Platform\Microsoft.Testing.Extensions.Hosting\Microsoft.Testing.Extensions.Hosting.csproj" />
<ProjectReference Include="..\..\..\..\src\Platform\Microsoft.Testing.Extensions.OpenTelemetry\Microsoft.Testing.Extensions.OpenTelemetry.csproj" />
<ProjectReference Include="..\..\..\..\src\Platform\Microsoft.Testing.Platform.MSBuild\Microsoft.Testing.Platform.MSBuild.csproj"
Condition=" '$(BuildingSolutionFile)' != 'true' "
ReferenceOutputAssembly="false"
PrivateAssets="all"
SetConfiguration="Configuration=$(Configuration)"
SetTargetFramework="TargetFramework=netstandard2.0" />
</ItemGroup>

<Import Project="..\..\..\..\src\Platform\Microsoft.Testing.Extensions.Hosting\buildMultiTargeting\Microsoft.Testing.Extensions.Hosting.props" />
<Import Project="..\..\..\..\src\Platform\Microsoft.Testing.Extensions.OpenTelemetry\buildMultiTargeting\Microsoft.Testing.Extensions.OpenTelemetry.props" />
<Import Project="Sdk.targets" Sdk="MSTest.Sdk" />
<Import Project="..\..\..\..\src\Platform\Microsoft.Testing.Extensions.Hosting\buildMultiTargeting\Microsoft.Testing.Extensions.Hosting.targets" />
<Import Project="..\..\..\..\src\Platform\Microsoft.Testing.Extensions.OpenTelemetry\buildMultiTargeting\Microsoft.Testing.Extensions.OpenTelemetry.targets" />
<Import Project="..\..\..\..\src\Platform\Microsoft.Testing.Platform.MSBuild\buildMultiTargeting\Microsoft.Testing.Platform.MSBuild.targets" />

</Project>
21 changes: 10 additions & 11 deletions samples/public/MTPHostIntegration/AspireTests/Program.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Reflection;

using Microsoft.Extensions.Hosting;
using Microsoft.Testing.Extensions;

HostApplicationBuilder builder = Host.CreateApplicationBuilder();
builder.AddTestServiceDefaults();
using Microsoft.Extensions.Logging;

using IHost host = builder.Build();
return await host.RunTestingPlatformAsync(args, tests =>
internal static class AspireTestHost
{
tests.AddMSTest(() => [Assembly.GetExecutingAssembly()]);
tests.AddTestingPlatformDiagnostics();
});
public static Task<IHost> CreateHost()
{
HostApplicationBuilder builder = Host.CreateApplicationBuilder();
builder.Logging.ClearProviders();
builder.AddTestServiceDefaults();
return Task.FromResult(builder.Build());
}
}
Loading
Loading