How do I replace settings appsettings.json related to my test projects.
There is File Trasnform task that does the variable substitution exactly like IIS Web App Deployment Task but in a separate step.
How do I force the test runner to use a specific environment, e.g Staging, so that appsettings.Staging.json will is picked?
Environment variables like ASPNETCORE_ENVIRONMENT can be configured in a .runsettings file:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<EnvironmentVariables>
<ASPNETCORE_ENVIRONMENT>Development</ASPNETCORE_ENVIRONMENT>
</EnvironmentVariables>
</RunConfiguration>
</RunSettings>
The value of environment variable can be then replaced in a separate pipeline step. I've used powershell task:
steps:
- powershell: |
$fileName = "$(System.DefaultWorkingDirectory)/MyBuildArtifact/test.runsettings";
[xml]$xml = Get-Content $fileName
$xml.SelectSingleNode("//ASPNETCORE_ENVIRONMENT").InnerText = "$(System.StageDisplayName)";
$xml.Save($fileName);
failOnStderr: true
showWarnings: true
displayName: 'Configure ASPNETCORE_ENVIRONMENT'
I also needed to make sure the test.runsettings file is included in my build artifact and configure the Visual Studio Test task to use that file