Ever wondered how you can run integration-, system- or UI tests in different environments as your artifacts get promoted through them.

Wonder no more.

What are runsettings?

Runsettings files are pieces of XML configuration that can be used when running a set of tests with VSTest. It does not matter what type of test we are dealing with. Runsettings work for all of them.

Here is an example of what a runsettings file might look like.

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<TestRunParameters>
<Parameter name="Url" value="http://ourbeautifulwebsite-dev.azurewebsites.net/" />
<TestRunParameters>
</RunSettings>

In this runsettings file we are specifying a URL for an Azure Web App. We might need this information to run UI tests after a deployment from VSTS or TFS. Obviously the URL is different in every environment, so hard coding it would be a solution – although not a very practical one in case environment names change or an extra environment is added.

If the test runner is given a runsettings file at the start of the test run, the settings are made available to use as a TestContext object.

private static string baseURL = string.Empty;

[AssemblyInitialize]
public static void Setup(TestContext context)
{

driver = new ChromeDriver();
if (context.Properties["Url"] != null) //Set URL from a build
{

baseURL = context.Properties["Url"].ToString();

}
driver.Manage().Window.Size = new Size(1920, 1080);

}

In this case I am choosing to set the URL at the assembly level, but I could do so at a class level as well.

If you mark a method as either AssemblyInitialize or ClassInitialize in MSTest, you will be forced to mark said method static and the method will need to receive a TestContext object.

This allows you to choose if you want to implement your test fixture at an assembly level or a class level.

In this case the test fixture is not necessarily the NUnit equivalent of a test fixture (a set of tests with the option to have static information) but the design pattern concept of a test fixture (static information held in memory for all tests or tests of a certain category to use during the test run).

Using a runsettings file in Visual Studio

In Visual Studio go to “Test” > “Test Settings” > “Select Test Settings File”.

During the next test run your runsettings file will be loaded and the settings will be available during runtime.

Do not forget to set your runsettings to “Copy always” so that they are available as an artifact for system testing.

Using a runsettings file in VSTS

There is an option to supply a runsettings file in the VSTest task. If you specify a runsettings file here, it will be passed to the VSTest runner at runtime.

Once again, in order for your runsettings file to be available as an artifact (to use during the release cycle) it needs to be copied into the build output folder. (and the artifacts staging folder – if different)

The relevant setting is in “Execution Options”.