If I set environment variables for a .Net Core web project in Visual Studio 2017 using the project properties page, I can read the value of the variable using Environment.GetEnvironmentVariable
; however, when I set the environment variable for my xUnit testing project and then debug the test, Environment.GetEnvironmentVariable
always returns null. Is there something about the fact that it is a testing project that should prevent the variable from being used the same as with the web project? If so, is there a way that I can set the environment variables for a test project? Thank you.
相关问题
- How to know full paths to DLL's from .csproj f
- Importing NuGet references through a local project
- Visual Studio 2019 - error MSB8020: The build tool
- 'System.Threading.ThreadAbortException' in
- VS2017 RC - The following error occurred when tryi
相关文章
- How to show location of errors, references to memb
- How to track MongoDB requests from a console appli
- Visual Studio Hangs on Loading UI Library
- DotNet Core console app: An assembly specified in
- How to use Mercurial from Visual Studio 2010?
- EF Core 'another instance is already being tra
- Re-target .NET Core to net471, net 472
- Copy different file to output directory for releas
The
GetEnvironmentVariable
works fine in xUnit tests. The problem is to properly set a variable. If you set the variable atProperties -> Debug
page, then the variable is written toProperties\launchSettings.json
and Visual Studio makes all work to launch an application with the selected profile. As you could see,launchSettings.json
even isn't copied to output folder by default. It's impossible to pass this file as argument todotnet run
ordotnet test
, that leads to obvious problem if tests are runned automatically on a CI server. So it is not surprising thatlaunchSettings.json
isn't considered by a test runner.Solution: there are a lot of ways to setup a test environment in xUnit:
For example, this collection fixture sets up all environment variables from
launchSettings.json
:Set
Copy to output directory: Always
forlaunchSettings.json
to make the file accessible from tests.