Describe the bug
When using dotnet test in Microsoft.Testing.Platform mode, -e / --environment appears to be accepted by the CLI, but the environment variable is not visible inside the test process.
This affects tests that build .NET host configuration from environment variables. For example, passing -e DOTNET_ENVIRONMENT=Development does not make Host.CreateApplicationBuilder() load appsettings.Development.json; the test process still sees DOTNET_ENVIRONMENT as unset.
Setting the same environment variable in the shell before invoking dotnet test works.
Related MTP tracking issue: #45927
To Reproduce
Minimal project:
global.json:
{
"test": {
"runner": "Microsoft.Testing.Platform"
}
}
MtpEnvRepro.Tests.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="xunit.v3.core.mtp-v2" Version="3.2.2" />
</ItemGroup>
</Project>
EnvironmentTests.cs:
using Xunit;
public sealed class EnvironmentTests
{
private static readonly string AtTypeInit =
Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "<null>";
[Fact]
public void Dotnet_environment_is_visible()
{
var duringTest =
Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "<null>";
Assert.Equal("Development", AtTypeInit);
Assert.Equal("Development", duringTest);
}
}
Run:
dotnet test --project MtpEnvRepro.Tests.csproj -e DOTNET_ENVIRONMENT=Development
Actual result: the test fails because DOTNET_ENVIRONMENT is <null>.
Also reproduced with:
dotnet test --project MtpEnvRepro.Tests.csproj --environment DOTNET_ENVIRONMENT=Development
This works as expected:
$env:DOTNET_ENVIRONMENT = 'Development'
dotnet test --project MtpEnvRepro.Tests.csproj
Expected result: variables passed through dotnet test -e/--environment should be visible to the MTP test process, or the option should not be accepted/advertised for this mode.
Exceptions (if any)
No exception. The test process runs, but the environment variable is missing.
Further technical details
Reproduced with:
- .NET SDK: 10.0.204
- MSBuild: 18.3.3
- Host runtime: 10.0.8
- OS: Windows 10.0.26200, win-x64
- Test runner mode:
global.json with "test": { "runner": "Microsoft.Testing.Platform" }
- xUnit package:
xunit.v3.core.mtp-v2 3.2.2
Describe the bug
When using
dotnet testin Microsoft.Testing.Platform mode,-e/--environmentappears to be accepted by the CLI, but the environment variable is not visible inside the test process.This affects tests that build .NET host configuration from environment variables. For example, passing
-e DOTNET_ENVIRONMENT=Developmentdoes not makeHost.CreateApplicationBuilder()loadappsettings.Development.json; the test process still seesDOTNET_ENVIRONMENTas unset.Setting the same environment variable in the shell before invoking
dotnet testworks.Related MTP tracking issue: #45927
To Reproduce
Minimal project:
global.json:{ "test": { "runner": "Microsoft.Testing.Platform" } }MtpEnvRepro.Tests.csproj:EnvironmentTests.cs:Run:
Actual result: the test fails because
DOTNET_ENVIRONMENTis<null>.Also reproduced with:
This works as expected:
Expected result: variables passed through
dotnet test -e/--environmentshould be visible to the MTP test process, or the option should not be accepted/advertised for this mode.Exceptions (if any)
No exception. The test process runs, but the environment variable is missing.
Further technical details
Reproduced with:
global.jsonwith"test": { "runner": "Microsoft.Testing.Platform" }xunit.v3.core.mtp-v23.2.2