Skip to content

dotnet test -e/--environment is not visible to Microsoft.Testing.Platform test process #54323

Description

@cmeeren

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions