AlexB
AlexB

Reputation: 4538

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.

Upvotes: 8

Views: 9445

Answers (2)

EdK
EdK

Reputation: 63

In your setup you can add: Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");

Upvotes: 2

AlexB
AlexB

Reputation: 4538

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.

Upvotes: 3

Related Questions