2
votes

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?

1
I suggest you use the keyvault for this kinf of situationMelchia
Yes. But even if I save it to the key vault and retrieve it back to DevOps variable, I am not able to set it to appSettings.Huodong
Why do you want to change Unit Test Project in release pipeline? Why do you need Unit Test project in release peipeline?Krzysztof Madej
@KrzysztofMadej The background is that our source code needs to be scanned by security vulnerabilities tools, if any hard-coded passwords found in source code, there will be an security warning, right? I want to pass the security checking, I set all hard-coded passwords to dummy one, then my unit test in DevOps fails. Yes I may went to a wrong direction at the beginning, but the answer give a right way to go - use runsettings. Should every question need to be technical exactly correct?Huodong

1 Answers

3
votes

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://docs.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"];
}