Huodong
Huodong

Reputation: 623

Set App Settings for Visual Studio Test Task in Azure DevOps Pipeline

I have a .Net Framework 4.6.2 Unit.Tests project, which has App.config file, in which it has some passwords in <appSettings> element. I want to update them in Azure DevOps build pipeline.

I'm using Visual Studio Test task to run the unit test. I tried to update appSettings in Execution options > Override test run parameters, but it didn't work as expected.

enter image description here
I know that Azure App Service deploy task can update appSettings in Application and Configuration Settings > App setttings. I want something similar in Visual Studio Test task. enter image description here
Any idea?

Upvotes: 3

Views: 2599

Answers (1)

Alex
Alex

Reputation: 18546

The "Override Test Run Parameters" section only applies to runsettings or testsettings files. It does not apply to app.config files.

Override parameters defined in the TestRunParameters section of runsettings file or Properties section of testsettings file. For example: -key1 value1 -key2 value2. Note: Properties specified in testsettings file can be accessed via the TestContext using Visual Studio 2017 Update 4 or higher

If you want to configure something in the app.config. Try using a "replace tokens" task (there are multiple options).

Our tests usually leverage the runsettings files. The file syntax is quite similar to the app.config, and you access the values via TestContext.

https://learn.microsoft.com/en-us/visualstudio/test/configure-unit-tests-by-using-a-dot-runsettings-file?view=vs-2019

  <!-- Parameters used by tests at run time -->
  <TestRunParameters>
    <Parameter name="webAppUrl" value="http://localhost" />
    <Parameter name="webAppUserName" value="Admin" />
    <Parameter name="webAppPassword" value="Password" />
  </TestRunParameters>
[TestMethod]
public void HomePageTest()
{
    string appURL = TestContext.Properties["webAppUrl"];
}

Upvotes: 4

Related Questions