5

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.

AlexB
  • 4,167
  • 4
  • 45
  • 117
  • Are you asking how to set environment variables, or how to set environment variables such that they only apply in your test environment? – Marc Talbot Dec 13 '17 at 01:29
  • Do you see Environment Variables section when you go to project properties and choose Debug tab? – Oleksii Aza Dec 13 '17 at 01:29
  • 1
    I saw it. And set it. And it doesn't work. Remember, this is a test project. – AlexB Dec 13 '17 at 01:46
  • 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. – AlexB Dec 13 '17 at 01:48
  • On windows go to command prompt and enter set ASPNETCORE_ENVIRONMENT=Development on macOS open terminal and enter export ASPNETCORE_ENVIRONMENT=Development – Mike Lunn Dec 13 '17 at 02:43
  • 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. – AlexB Dec 13 '17 at 16:41
  • @alvipeo this is a common issue. Here check this out https://github.com/aspnet/Tooling/issues/456#issuecomment-333787485 – Ed C Dec 13 '17 at 20:57
  • you can always create an additional profile. that's what i always do – Neville Nazerane Dec 13 '17 at 23:47
  • Does this answer your question? [Should GetEnvironmentVariable Work in xUnit Test?](https://stackoverflow.com/questions/43927955/should-getenvironmentvariable-work-in-xunit-test) – Ian Kemp Feb 21 '20 at 07:26
  • 3 years later I don't actually care – AlexB Feb 21 '20 at 17:15

2 Answers2

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://stackoverflow.com/a/43951218/2896495.

AlexB
  • 4,167
  • 4
  • 45
  • 117
  • @NevilleNazerane setting ASPNETCORE_ENVIRONMENT does not work for me in either pre- or post-build event – rajniszp Aug 16 '22 at 15:36
1

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

EdK
  • 53
  • 5