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

2

7

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.

Methenamine answered 13/12, 2017 at 1:8 Comment(10)
Are you asking how to set environment variables, or how to set environment variables such that they only apply in your test environment?Percept
Do you see Environment Variables section when you go to project properties and choose Debug tab?Pointsman
I saw it. And set it. And it doesn't work. Remember, this is a test project.Methenamine
Marc, I'm asking how to actually set up ASPNETCORE_ENVIRONMENT variable when running tests. I tried Debug section in the properties and launchSettings.json. Nothing works ... for me.Methenamine
On windows go to command prompt and enter set ASPNETCORE_ENVIRONMENT=Development on macOS open terminal and enter export ASPNETCORE_ENVIRONMENT=DevelopmentFortunio
Yeah, that's new :) But thanks anyway. I really hope asp.net core tooling team reads this and I hope test project settings will have effect in the near future. Because I work on different projects and some of them run in dev mode, some in release. It's not very convenient to do this from command line when you have two places in VS2017 where you can set them.Methenamine
@alvipeo this is a common issue. Here check this out github.com/aspnet/Tooling/issues/456#issuecomment-333787485Acicular
you can always create an additional profile. that's what i always doHolohedral
Does this answer your question? Should GetEnvironmentVariable Work in xUnit Test?Lebron
3 years later I don't actually careMethenamine
M
3

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://mcmap.net/q/298958/-should-getenvironmentvariable-work-in-xunit-tests.

Methenamine answered 13/12, 2017 at 23:11 Comment(1)
@NevilleNazerane setting ASPNETCORE_ENVIRONMENT does not work for me in either pre- or post-build eventCorri
O
2

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

Ornas answered 29/6, 2023 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.