Select Page

In this article we tackle all of the common challenges encountered when trying to do continuous integration with Azure DevOps aka VSTS aka TFS. Some critical topics that are tackled here are:

• How to work with Azure DevOps environment variables
• How to create a build pipeline

By the way, if you aren’t aware, TFS, VSTS and Azure DevOps are all technically the same solution. Over several years, Microsoft did a lot of rebranding that created the confusion between all of these products. Hence it went from TFS to VSTS to Azure DevOps relatively quickly. Causing lots of confusion. The latest version is called Azure DevOps. I hope that it stays with this name for the foreseeable future 🙏

## How to set Sauce Labs environment variables in Azure DevOps with .NET Core?

These are instructions if you are working with .NET Core

1. Create environment variables in Azure DevOps

Create the environment variables in ADO and assign values to those variables.

Here’s an example

My recommendation is that you name the variables to something similar that I have above. Do not name your ADO variables the same as your Environment variables as that will cause you issues when you are trying to read them. So don’t name your ADO variable SAUCE_USER_NAME for example

2. Make sure you are reading values from environment variables in your code

For this to work, you need to be reading values into environment variables exactly like this:

var sauceUserName = Environment.GetEnvironmentVariable("SAUCE_USERNAME");
var sauceAccessKey = Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY");

3. Configure your YAML to read values from VSTS aka Aure DevOps and set them into system environment variables

Your YAML needs this piece of code to be able to set environment variables of the Azure DevOps box

#need to use the vso tasks so that the env variables persist trhough tasks in ADO
- powershell: |
Write-Host "Our Sauce Username in ADO is=> $($env:SAUCE_USER)";
Write-Host "Our Sauce Access Key in ADO is=> $($env:SAUCE_KEY)";
Write-Host ("##vso[task.setvariable variable=SAUCE_USERNAME]$($env:SAUCE_USER)")
Write-Host ("##vso[task.setvariable variable=SAUCE_ACCESS_KEY]$($env:SAUCE_KEY)")

Here is the full YAML file that works.

## How to set Sauce Labs environment variables in Azure DevOps with .NET Framework?

There might be a few ways to do this, but I found that using a Powershell script works fine for me.

### Using a Powershell script

1. First you need to create some environment variables in your Azure DevOps UI that you want to use for values. This is an example of a variable that I would like to set on the test agent for my automation scripts. For example sauce.userName. I will use the value of this variable(sauce.userName) and have a Powershell script set it in my System Environment Variables of the test agent when my automation is running. That way, the value of this variable isn’t exposed to the public.

2. Next, you will want to create a Powershell script that you attach to your solution. Here’s my solution layout.

Don’t forget to make sure to copy your Powershell script to output directory on build in your .csproj

Here is what you want in your Powershell script.

Param(
[string]$sauceUserName, [string]$sauceAccessKey,
[string]$rdcVodQaNativeAppApiKey, [string]$rdcSauceDemoIosRdcApiKey
)
Write-Output "sauce.userName value from ADO was passed as a Argument in the ADO Task called $env:SAUCE_USERNAME " + "to sauceUserName variable in the Posh. This is the value found=>$sauceUserName"
Write-Output "sauce.accessKey that was passed in from Azure DevOps=>$sauceAccessKey" Write-Output "sauce.rdc.VodQaNativeAppApiKey stored in Azure DevOps=>$rdcVodQaNativeAppApiKey"
Write-Output "sauce.rdc.SauceDemoIosRdcApiKey stored in Azure DevOps=>$rdcSauceDemoIosRdcApiKey" [Environment]::SetEnvironmentVariable("SAUCE_USERNAME", "$sauceUserName", "User")
[Environment]::SetEnvironmentVariable("SAUCE_ACCESS_KEY", "$sauceAccessKey", "User") [Environment]::SetEnvironmentVariable("VODQC_RDC_API_KEY", "$rdcVodQaNativeAppApiKey", "User")

sauce.AccessKey = $env:SAUCE_ACCESSKEY word.a.b.c = WORD_A_B_C ## All about YAML Super important YAML schema ### Working YAML file for UI Automation ### Possibly a useful YAML for test automation I found this YAML snippet for executing tests with VSTest from here Not sure if it’s helpful yet, but it might be. Posh script # Create a secret variable - powershell: | Write-Host '##vso[task.setvariable variable=mySecret;issecret=true]abc' # Attempt to output the value in various ways - powershell: | # Using an input-macro: Write-Host "This works:$(mySecret)"

# Using the env var directly:
Write-Host "This does not work: $env:MYSECRET" # Using the mapped env var: Write-Host "This works:$env:MY_MAPPED_ENV_VAR"
env:
MY_MAPPED_ENV_VAR: \$(mySecret)

How to run NUnit tests in VSTS

#### Filtering Tests

Instead of using Nunit test filters, you need to use the MSTest filters. For example, you need to convert:

cat == unit to /TestCaseFilter:”Category=unit”

This will execute all Nunit tests that have the Category “unit”.