Photo by Nicolas Thomas on Unsplash

Passing parameters to NUnit tests using Azure Pipelines

Stephan Bester
3 min readAug 23, 2022

--

There are various use cases for passing parameters to our tests, e.g. providing environmental configuration. In this guide, I will demonstrate how this can be achieved in Azure Pipelines using NUnit as a test framework.

In your solution

Create a local .runsettings file

In the root of your solution, create an XML file named .runsettings which will define the parameters for your tests. Below is an example showing settings for shipName and shipDesignation:

Example .runsettings file containing test run parameters

Here, you can specify the default values for your test parameters, like the ones you’d use when running the tests locally.

This file needs to have the exact name “.runsettings” and be located in the root of your solution in order for it to be picked up by Visual Studio automatically. Confirm that Visual Studio is configured to automatically detect the run settings file for your solution. Choose Options from the Tools menu. Under the General section of the Test node, tick the box for “Auto Detect runsettings Files”.

Auto Detect runsettings Files in the Options dialogue

Warning: It seems that even after doing this, the .runsettings file still needs to appear with a checkmark under Configure Run Settings in the Test menu, e.g.

Configure Run Settings under Test

As the menu options suggest, you can also select a file manually.

Retrieve parameters in the test

To access such a parameter in your NUnit test, use the parameter name as an index to the Parameters property of the static class TestContext, e.g.

var name = TestContext.Parameters["shipName"];
var designation = TestContext.Parameters["shipDesignation"];

In your Azure Pipelines template

Use the .NET Core CLI task

For the test step in your pipeline YAML, use the .NET Core CLI task (e.g. DotNetCoreCLI@2) to call the dotnet test command. This command supports passing runsettings arguments as documented here by Microsoft.

Passing test run parameters as arguments

As the arguments to this task, pass the TestRunParameters as per the Microsoft example. The end result needs to have a backslash followed by a double quote each time, as reported by the .NET CLI itself:

Format: TestRunParameters.Parameter(name=\"<name>\", value=\"<value>\")

Note: When writing this in YAML, you have to escape both the backslashes and the double quotes. Kudos to Ian Ferguson for posting an answer to his own question on StackOverflow and saving me a lot of trial and error.

Here is an example of a build step that does just this:

Example step in Azure Pipelines that passes test run parameters to the .NET CLI

Here is an example that passes more than one test run parameter:

And here is an example that takes the test run parameter value from a pipeline template parameter:

Example that passes a test run parameter from a pipeline parameter

Used for this article

  • .NET 6
  • NUnit 3.13.3 (NuGet)
  • NUnit3TestAdapter 4.2.1 (NuGet)
  • NUnit.Analyzers 3.3.0 (NuGet)
  • Visual Studio Enterprise 2022

--

--

Stephan Bester

Software developer walking the edge between legacy systems and modern technology. I also make music: stephanbmusic.com