Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I use this code to load settings depending on Environment in my xUnit test project:

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");
    }
}

But whatever I do I get envVariable empty. Where can I set ASPNETCORE_ENVIRONMENT for test project (VS2017)?

To ASP.NET Core Tooling Team

Please make test project settings work for environment. Thank you.

like image 906
alvipeo Avatar asked Oct 29 '22 22:10

alvipeo


1 Answers

ok, on Windows set ASPNETCORE_ENVIRONMENT with this command:

setx ASPNETCORE_ENVIRONMENT Development

so the value stays (notice the 'setx').

Also, look at this answer https://stackoverflow.com/a/43951218/2896495.

like image 147
alvipeo Avatar answered Nov 03 '22 03:11

alvipeo