-
Notifications
You must be signed in to change notification settings - Fork 329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing the ASP.NET Core Environment to the Dotnet Test Runner #669
Comments
@RehanSaeed as a workaround on Mac & Linux you can do this: Unfortunately, it doesn't work on Windows. |
Ho thanks @dzirkler, it means I can do this on Bash on Windows 10 that's cool. |
Does not the following workaround work in cmd?
|
This commands work for me:
($env:ASPNETCORE_ENVIRONMENT="Staging") | dotnet test -v n
Set "ASPNETCORE_ENVIRONMENT=Staging" && dotnet test -v n In both cases environment variable ASPNETCORE_ENVIRONMENT is set for current session only - you can check that it is right by opening other powershell/bat windows and type:
(get-item env:ASPNETCORE_ENVIRONMENT).Value
echo %ASPNETCORE_ENVIRONMENT% |
What's the meaning of setting an environment variable as I am also looking to have some |
Is there another way to define inside some json file, which environment variables will be passed to the application when running from a test runner? When running the ASP.NET Core projects, who is responsible for reading the Is there a way to load these vars (from |
Using the variables defined in launchSettings.json would be perfect. Is it possible? |
That seems to be a murky subject. From another issue which I can't find right now, the support seems like it should exist, but folks are commenting that it doesn't work. |
I have stumbled upon this yesterday. although it drove me mad, it seems reasonable that tests should not rely on the environment. I have used this library to circumvent the issue. |
Summarizing, and correcting the information in this thread so we have a clearer way going forward.
This is true. It will add the environment variable to the current environment variables only for the current command (
This is partially true. On Windows you can set environment variable for Process, User or Machine, but not for current command. Workarounds exists, but they require capturing all variables before command run and resetting them after (see an implementation of this in PowerShell). On WindowsOn Windows we are able to set environment variables per process, user and (given we have enough permissions) per machine. The default is Process, and the standard way to do that in PowerShell is using the Environment drive: $env:ASPNETCORE_ENVIRONMENT="Staging" This will set the variable for the current process, and every process that will start from this process (child process) will inherit all of the environment variables, including this one. Alternatively in cmd SET ASPNETCORE_ENVIRONMENT=Staging (We can confirm the variable was set by querying it Inheriting variablesOn Windows a process will inherit all environment variables from parent when it starts. We can confirm that easily by starting a powershell instance, removing all env variables except PATH and then running cmd and dump all env variables (PowerShell is not happy when we start from such a bare environment). PS> Get-ChildItem Env:\
Name Value
---- -----
...etc
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFiles(x86) C:\Program Files (x86)\Common Files
CommonProgramW6432 C:\Program Files\Common Files
... etc
PS> Get-ChildItem Env:\ | where Name -ne PATH | Remove-Item
PS> cmd.exe /c SET
COMSPEC=C:\WINDOWS\system32\cmd.exe
Path=C:\Python38\Scripts\;C:\Python38\;C:\Program Files\Common Files....
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.JS;.WS;.MSC
PROMPT=$P$G The Running dotnet testIn PowerShellThe proposed workaround works, but is not ideal because it relies on a PowerShell quirk. # works
($env:ASPNETCORE_ENVIRONMENT="Staging") | dotnet test -v n
# - DO NOT do this
$env:ASPNETCORE_ENVIRONMENT="Staging" | dotnet test -v n A better way to do this is simply set the variable on it's own line. $env:ASPNETCORE_ENVIRONMENT="Staging"
dotnet test
dotnet test
dotnet test Or for visibility, and easier editing on re-runs put the two commands on the same line and terminate the first one by the command terminator $env:ASPNETCORE_ENVIRONMENT="Staging"; dotnet test
# or even
$env:ASPNETCORE_ENVIRONMENT="Staging"; $env:SOME_OTHER_VARIABLE=10; dotnet test Remember that the variables are still set per process, you are just running multiple commands on the same line. In cmdHere is the same thing in cmd, joined by SET ASPNETCORE_ENVIRONMENT=Staging& SET SOME_OTHER_VARIABLE=10& dotnet test ⚠ Notice that there are no quotes around the value and no spaces after the value. The value is taken literally, and will contain REM - DO NOT do this
SET ASPNETCORE_ENVIRONMENT="Staging" & ECHO '%ASPNETCORE_ENVIRONMENT%'
'"Staging" ' Running with settings from launchSettings.jsonAs a quick prototype I parsed the file and tried to start {
"profiles": {
"UnitTestProject1": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Staging"
}
}
}
} using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Assert.AreEqual("Staging", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
}
}
} $projectName = "UnitTestProject1"
$launchSettingsPath = "Properties/launchSettings.json"
$startInfo = [Diagnostics.ProcessStartInfo]::new()
$startInfo.UseShellExecute = $false
$startInfo.CreateNoWindow = $false
$variablesToKeep = $startInfo.EnvironmentVariables | where {
$_.Name.ToUpperInvariant() -in @(
'PATH', 'HOME', 'HOMEDRIVE',
'HOMEPATH', 'USERPROFILE',
'PROGRAMFILES', 'PROGRAMDATA',
'APPDATA'
)
}
# DO NOT do this. uncomment to clear all variables, dotnet will not start
# $startInfo.EnvironmentVariables.Clear()
if (Test-Path $launchSettingsPath) {
$launchSettings = Get-Content $launchSettingsPath | ConvertFrom-Json
$variables = $launchSettings.profiles."$projectName".environmentVariables
foreach ($p in $variables.PSObject.Properties) {
$startInfo.EnvironmentVariables.Add($p.Name, $p.Value)
}
}
# DO NOT do this. uncomment to add the kept variables after claring, dotnet test will start but fail
# to setup communication with the underlying processes
# foreach ($v in $variablesToKeep) {
# $startInfo.EnvironmentVariables.Add($v.Name, $v.Value)
# }
# this would be better but does not work
# $startInfo.EnvironmentVariables.Add('NUGET_PACKAGES', (Resolve-Path '~/.nuget/packages').ProviderPath)
'Starting with:'
$startInfo.EnvironmentVariables
$startInfo.FileName='dotnet'
$startInfo.Arguments = 'test'
$startInfo.WorkingDirectory = $pwd.Path
$process = [Diagnostics.Process]::new()
$process.StartInfo = $startInfo
$process.Start() This will pass as long as we are passing all environment variables. So it could be a viable workaround until this is properly implemented. 🥳🥳🥳 |
Update: revisited this proposal to reflect the UX guideline of dotnet cli I identified these issues in this thread: Set environment variables for
|
That is correct. Link. |
Note to self: Use |
This is partly done. It's possible to pass environment variables into the |
.runsettings is supposed to have an XML section for environment variables, and while Microsoft has documented that, it doesn't seem to work, and I find almost nothing on it: A unit test in this repository has: var runsettingsXml = @"<RunSettings>
<RunConfiguration>
<EnvironmentVariables>
<RANDOM_PATH>C:\temp</RANDOM_PATH>
</EnvironmentVariables>
</RunConfiguration>
</RunSettings>"; but that gives me: TpTrace Warning: 0 : 12628, 4, 2021/03/11, 16:23:38.052, 270394899679, testhost.dll, Invalid settings 'RunConfiguration'. Unexpected XmlElement: 'EnvironmentVariables'. Thoughts? I want something that runs in Visual Studio as well as something I can run in Jenkins. |
This is a new feature and won't be implemented, we are focusing on adding new features to Testing.Platform instead. https://aka.ms/testingplatform |
Moved from xunit/xunit#857. It would be incredibly useful if I could do something like this in my functional tests:
Instead of having to set the
ASPNETCORE_ENVIRONMENT
environment variable before running the test and then resetting it at the end of the test. The other advantage is that you can run more than one set of tests at the same time which is important for build machines.The text was updated successfully, but these errors were encountered: