Search code examples
azure-devopsvstest

Secrets as environmental variables in vstest


In my tests I'm using environmental variable. Due to security reasons, when setting it in azure devops pipeline I need to mark it as secret. Is it possible to pass it as argument, within vstest task?


Solution

  • I've been through this. There's no way to pass variables to VSTest on the command line, which means you have to jump through a few hoops.

    You have a few options:

    • Use a runsettings file with a TestRunParameters section, then access it via TestContext.Properties["variableName"] within the tests themselves. You can use standard token replacement patterns to transform the XML file.

    • Use an app.config or appsettings.json (depending on your platform). This works pretty much the same as above, except, of course, you use the standard configuration classes to retrieve the values.

    • Add a step to your pipeline that sets the appropriate environment variables. Secrets don't get automatically mapped to environment variables for security purposes, but there's nothing that's stopping you from doing it yourself.

    • Move the secret values into a keyvault or some other sort of external secret storage and configure the test to pull the secrets at runtime.