在 VS2017 中使用 xUnit 测试 .NET Core 2.0 class 库时如何设置 ASPNETCORE_ENVIRONMENT

How to set ASPNETCORE_ENVIRONMENT when testing .NET Core 2.0 class library with xUnit in VS2017

我使用此代码根据我的 xUnit 测试项目中的环境加载设置:

public class TestSettings
{
    public string AzureConnectionString { get; }

    public TestSettings(ITestOutputHelper output)
    {
        string envVariable = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

        output.WriteLine($"env: '{envVariable}'");

        IConfigurationRoot config = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddJsonFile($"appsettings.{envVariable}.json", optional: true)
            .Build();
        AzureConnectionString = config.GetConnectionString("AzureStorage");
    }
}

但无论我做什么,我都会 envVariable 空空如也。我在哪里可以为测试项目(VS2017)设置ASPNETCORE_ENVIRONMENT

致 ASP.NET 核心工具团队

请使测试项目设置适用于环境。谢谢。

好的,在 Windows 上使用此命令设置 ASPNETCORE_ENVIRONMENT

setx ASPNETCORE_ENVIRONMENT Development

所以值保持不变(注意 'setx')。

另外,看看这个回答