How to access TestRunParameters within RunSettings file

35,951

Solution 1

For those that use Resharper with this issue, I discovered the fix (no need to disable Resharper):

  1. Go to Visual Studio top menu -> Resharper -> Options

  2. Find the Tools section, expand "Unit Testing"

  3. Click on "MsTest". The checkbox should be on enabled, but the Test Settings file path below that may be blank. If it is, click on browse and select the runsettings file you want to use.

  4. Click save, rebuild and try to run the tests, the parameters should now work.

Not sure why but simply selecting the Test Settings file from the Tests menu -> Test Settings does not actually work when using Resharper, so this file needs to be explicitly pointed to directly in the Resharper options.

Solution 2

I also came across this recently, as we wanted to move away from legacy environment variable usage. The solution provided below was suitable for our needs, but there may be a better one...

It turns out you can access these in the TestContext of a ClassInitialize method of your Test fixture. There is a Properties dictionary which has these parameters, and you could access the values here:

[ClassInitialize]
public static void TestClassinitialize(TestContext context)
{
    var webAppUrl = context.Properties["webAppUrl"].ToString();
   //other settings etc..then use your test settings parameters here...
}

Note: these are static so if you need access to this you may need to set up static properties to access within your Test code.

An alternative suggested is using a Data Driven Test approach. Here's some basic info on data driven tests here which may also help: https://msdn.microsoft.com/en-us/library/ms182527.aspx

As I said, the above solution suited our needs at a basic level.

UPDATE: see image below in response to test settings returning null...

Test Settings in Visual Studio

Solution 3

This works for me (VS2017-pro):

namespace TestApp.Test
{
    [TestClass]
    public class UnitTest1
    {
        // This enables the runner to set the TestContext. It gets updated for each test.
        public TestContext TestContext { get; set; }
        [TestMethod]
        public void TestMethod1()
        {
            // Arrange
            String expectedName = "TestMethod1";
            String expectedUrl = "http://localhost";
            // Act
            String actualName = TestContext.TestName;
            // The properties are read from the .runsettings file
            String actualUrl = TestContext.Properties["webAppUrl"].ToString();
            // Assert
            Assert.AreEqual(expectedName, actualName);
            Assert.AreEqual(expectedUrl, actualUrl);
        }
        [TestMethod]
        public void TestMethod2()
        {
            // Arrange
            String expectedName = "TestMethod2";
            // Act
            String actualName = TestContext.TestName;
            // Assert
            Assert.AreEqual(expectedName, actualName);
        }
    }
}

Make sure to select the runsettings file you wish to use, here: Test -> Test Settings.

Solution 4

I was trying to do this exact thing as well. As many of you may know, running tests through MTM exposes some additional properties to the TestContext, including the name of the Run Settings used. I used this property as a "Foreign Key" of sorts for our test data, allowing us to specify environment URLs etc. without hardcoding them or using the incredibly lackluster "Data Driving" tools that come with out of the box testing.

Of course, there's no way to expose any run-time properties when executing tests as part of a BDT or release workflow besides what @kritner is attempting which microsoft describes HERE. However if you read the comments of that link you'll discover what you may be able to infer here:

  • You need to use VS 2013 R5 or VS 2015 to use this solution
  • It will only work for Unit Tests!

Those of us who are trying to execute UI or Load tests as part of a CI or CD workflow are completely screwed. You don't get any additional properties in testContext, even when executing a Plan/Suite with certain test configurations (not settings) created in MTM. @Adam may have been able to get this to work when running vs debugging, but that may have only worked with unit tests. Through CodedUI I've been unable to retrieve the properties without getting a NullReferenceException. Here's an example of the janky code I was using to investigate:

if (testContextInstance.Properties["__Tfs_TestConfigurationName__"] != null) //Exposed when run through MTM
{
TFSTestConfigurationName = testContextInstance.Properties["__Tfs_TestConfigurationName__"].ToString();
}
else TFSTestConfigurationName = "Local"; //Local
var configName = testContextInstance.Properties["configurationName"] ?? "No Config Found";
Trace.WriteLine("Property: " + configName);

And the XML of my .runsettings file:

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
  <!-- Parameters used by tests at runtime. These are required as a substitute for TFS/MTM test settings.-->
  <!-- File instructions: https://msdn.microsoft.com/en-us/library/jj635153.aspx#example -->
  <!-- TFS instructions: https://blogs.msdn.microsoft.com/visualstudioalm/2015/09/04/supplying-run-time-parameters-to-tests/ -->  
  <TestRunParameters>
    <Parameter name="configurationName" value="Local" />
  </TestRunParameters>
</RunSettings>

And an excerpt from the .trx produced by the BDT workflow:

Property: No Config Found 

Solution 5

An alternative to disable Resharper is to enable MSTest support and select the test setting file on Resharper Options dialog (->Tools->Unit Testing->MsTest).

Share:
35,951

Related videos on Youtube

Kritner
Author by

Kritner

Just a boring Dad/Application Developer. I enjoy gaming, learning new technologies, teaching/problem solving, reading, music, movies, and potentially other stuff. That's about it. If you for some reason need me, feel free to tweet me at @RLHammett

Updated on July 09, 2022

Comments

  • Kritner
    Kritner over 1 year

    Reading through https://msdn.microsoft.com/en-us/library/jj635153.aspx I have created a .RunSettings files with a few parameters similar to the example:

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

    I plan on having a .RunSettings file for each of our environments with appropriate URLs and credentials for running a CodedUI test on the specified RunSettings file's environment.

    I can see that from command line to reference the Settings file I can run:

    vstest.console myTestDll.dll /Settings:Local.RunSettings /Logger:trx
    vstest.console myTestDll.dll /Settings:QA.RunSettings /Logger:trx
    

    etc...

    But I don't see any way that calls out how to actually utilize the TestRunParameters from within the codedUI test.

    What I would like to do is set up test initializers that use the TestRunParameters to determine where to log in, and what credentials to use. Something like this:

    [TestInitialize()]
    public void MyTestInitialize()
    {
        // I'm unsure how to grab the RunSettings.TestRunParameters below
        string entryUrl = ""; // TestRunParameters.webAppUrl
        string userName = ""; // TestRunParameters.webAppUserName
        string password = ""; // TestRunParameters.webAppPassword
        LoginToPage(entryUrl, userName, password);
    }
    public void LoginToPage(string entryUrl, string userName, string password)
    {
        // Implementation
    }
    

    Information on how to reference the TestRunParameters is greatly appreciated!

    EDIT

    /// <summary>
    /// Summary description for CodedUITest1
    /// </summary>
    [CodedUITest]
    public class CodedUITest1
    {
        public static string UserName = string.Empty;
        [ClassInitialize]
        public static void TestClassInitialize(TestContext context)
        {
            UserName = context.Properties["webAppUserName"].ToString();
            Console.WriteLine(UserName);
        }
        [TestMethod]
        public void CodedUITestMethod1()
        {
            this.UIMap.RecordedMethod1();
            // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
        }
        // Rest of the default class - TestContext instantiation, UI map instantiation, etc
    }
    

    The exception I'm getting when running:

    NullReference Exception

    @williamfalconeruk I have updated my test class as above, but I am still getting the same error, any idea what I'm doing wrong?

    • steve16351 over 8 years
      I have the same problem as you in VS2013 (the TestRunParameters not showing up in the TestContext properties). Looking at the MSDN example, msdn.microsoft.com/en-GB/library/jj635153(v=vs.120).aspx, I noticed that the TestRunParameters section isn't specified until you change the version to VS2015, so perhaps it's a new feature there.
    • Kritner
      Kritner over 8 years
      @steve16351 ah interesting, i had not noticed that. I wonder if that's the problem I'm having - I'm currently using VS2012. I'll have to give it a shot in 2015
    • Kritner
      Kritner over 8 years
      @steve16351 actually scratch that. I can tell in the screenshot above that that was on 2015 since I can see the code lens information. Still having the issue on VS2015
    • Ruslan over 7 years
      it's the Resharper test runner, who is ignoring your *.runsettings
    • Kritner
      Kritner over 7 years
      @Tsar I don't have Resharper :(
  • Kritner
    Kritner over 8 years
    Hmm my initializer does not take in a textcontext, and attempting to grab the property from the class textcontext throws a nullreference. Are you doing something specific to get a initializer with that signature? And how are you calling it with a provided context?
  • williamfalconeruk over 8 years
    note the Annotation [Classinitialize] - this is when the test class is set up to run and is in a static context, the [Testinitialize] annotation is used is called each time a test is run as an instance - thats the key difference.
  • williamfalconeruk over 8 years
  • williamfalconeruk over 8 years
    ok - have you set the test .runsettings file in the Visual Studio menu Test > Test Settings > Select Test Settings File ? this is how i've done it to debug...
  • Kritner
    Kritner over 8 years
    Yeah, unfortunately I had already performed that step when I got the issue. Not sure what else it could be. I upvoted your answer but am not going to accept yet so hopefully the thing I'm missing will be pointed out by someone... eventually :O thanks for the start anyway!
  • AdrianHHH
    AdrianHHH over 8 years
    @Kritner Clicking on the "Select test settings file" menu item should pop up a file choosing box. After choosing a file you should see the chosen file named and ticked in the menu.
  • AdrianHHH
    AdrianHHH over 8 years
    For completeness, there is another places where testsettings files can be set. The context menu for ".testsetting" files in solution explorer has a tickable "Active Web and Load Test Settings" entry. This one only applies to web and load tests. I have no idea why Visual Studio has two very different ways of specifying these files.
  • williamfalconeruk about 8 years
    @Kritner did you manage to get this worked out in the end - note i am still on VS2013 (but should be the same)
  • Kritner
    Kritner about 8 years
    @williamfalconeruk Unfortunately no, never did get this working.
  • Kritner
    Kritner almost 8 years
    Unfortunately I don't have resharper. It's Unfortunate that I don't have it, and unfortunate because I don't have it, it can't be my issue. :(
  • Kritner
    Kritner almost 8 years
    Unfortunately I'm on VS2015 and still experiencing the issue.
  • chief7
    chief7 almost 8 years
    I'm also experiencing this in VS2015. I have set my RunSettings file.
  • Adam
    Adam over 7 years
    I found that it ONLY works when you use the VS Test Explorer - Run Tests, NOT when you Debug Tests or use R# Test Runner. My setup is VS2015.Update 2. with R# Ultimate 10.0.1