Run Time Parameters with VSTest

Microsoft came out with a nice feature in Visual Studio 2015. It is called Run Time Parameters. The first blog post was posted on Sept 4th, 2015 here. A scenario is inside VSTS. A user can setup a build with run time parameters to switch a browser and/or web URL for automated testing.

First, a user needs to create a .runsettings file. Unfortunately, Visual Studio does not have a GUI to create one. Here is the Microsoft documentation on how to create a .runsettings file. The main area is the TestRunParameters section of the xml.

<TestRunParameters>  
  <Parameter name="webAppUrl" value="http://localhost" />
  <Parameter name="webAppUserName" value="Admin" />
  <Parameter name="webAppPassword" value="Password" />
</TestRunParameters>

Here is where you can create dynamic parameters that can be altered at run time. There is one flaw though. If you would like to use a .testsettings file, you will need to enabled ForcedLegacyMode which disables the TestRunParameters feature.

Once a .runsettings file is created in Visual Studio, you will need to select it inside the GUI.

After that, you can grab the parameters supplied inside the code using

string url = TestContext.Properties["webAppUrl"].ToString();  

I would suggest adding a static method(s) when grabbing these values. If a user doesn't select the test setting option in GUI, then they get a null reference exception. It is beneficial to give them a message to supply the test setting.

/// <summary>
/// Gets a string from the TestContext properties
/// </summary>
/// <param name="context">Test Context</param>
/// <param name="property">Property to find</param>
/// <returns>A string</returns>
    public static string GetString(TestContext context, string property)
    {
        if (context.Properties[property] == null)
        {
            throw new ArgumentException("Property does not exist, does not have a value, or a test setting is not selected");
        }
        else
        {
            return context.Properties[property].ToString();
        }
    }

Now we want to set it into a build. Inside the Test Assemblies step, we can edit the desired information.

Once you supply a .runsettings file, you can now use the Override TestRun Parameters and modify the parameters as needed.

I also reached out to the NUnit team to add this functionality into the NUnit Test Adapter here. It is on Charlie's radar and will hopefully be coming soon.