5

Following the procedures outlines here and here I was able to set a TestRunParameter and access it at run time in a Unit test. I then repeated the exact same process in a coded UI test but am not able to access the Properties.

My .runsettings file:

<RunSettings>
  <TestRunParameters>
    <Parameter name="webAppUrl" value="http://localhost" />
  </TestRunParameters>
</RunSettings>

My Test Method:

[TestMethod]
public void MyTest1()
{
    // This throws an error because Properties["webAppUrl"] is null
    string webAppUrl = TestContext.Properties["webAppUrl"].ToString();

    // etc...
}

Does a Coded UI test need additional configuration to access these run time properties?

Edit: I notice that within the context of a Unit test, the TestContext is Microsoft.VisualStudio.TestPlatform.MSTestFramework.TestContextImplementation. In the Coded UI test, the TestContext is Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapterContext.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Jonathan Eckman
  • 2,071
  • 3
  • 23
  • 49
  • It looks like it was added in an update to Visual Studio 2013 http://stackoverflow.com/a/33362632/155892 – Mark Sowul Nov 25 '15 at 14:45
  • I believe the feature is just flaky. I'm having problems reliably accessing the Properties in *unit* tests. Sometimes my properties from the runsettings file are there, sometimes they are not. – BrettJ May 13 '16 at 13:49

2 Answers2

4

The parameters defined in .runsettings TestRunParameter section can't be accessed in Coded UI test. When you debug the Coded UI test, you will find that TextContext.Properties contains some values, ResultsDirectory, AgentId and etc.

However, parameter defined in the TestRunParameter section can't be found.

enter image description here

Instead of setting parameters in TestRunParameter section, you can create a .cvs or .xml file, and access the data via data-driven. Check this article for the details:

https://msdn.microsoft.com/en-us/library/ee624082.aspx

Vicky - MSFT
  • 4,970
  • 1
  • 14
  • 22
  • I needed to use the Properties so that I could use different values depening on whether the test was run in dev or in an integration environment. From with I can see, the data in data-driven tests cannot be changes at runtime. This is what I was trying to accomplish: http://blogs.msdn.com/b/visualstudioalm/archive/2015/09/04/supplying-run-time-parameters-to-tests.aspx Can data-driven tests be used in this way? – Jonathan Eckman Sep 18 '15 at 00:01
  • @SystemAccount, you're right that data-driven test can't be changed during runtime. For your requirement, you can coding some logic to choose different value based on different environment. For using TestRunParameter for Coded UI test, you can consider submitting one feature request about it on the Microsoft UserVoice site: https://visualstudio.uservoice.com/forums/121579-visual-studio/category/30925-team-foundation-server-visual-studio-online – Vicky - MSFT Sep 18 '15 at 03:06
  • @Vicky-MSFT MS should show that CodedUI is important to them and they are going to support and improve it just like they do frequently with other products, other these small nagging issues are enough for us to move over to other ui test framework. – SarkarG Dec 23 '16 at 05:32
  • 1
    How can I send a parameter to a Coded UI test via TFS2015 build definitions? I was hoping I could use TestRunParameters, but then I suddenly ran into this limitation. – Mike Asdf Feb 17 '17 at 00:39
0

Try Using it in ClassInitialize inplace of Test Method refer below code

[ClassInitialize]
public static void TestClassinitialize(TestContext context)
{
    var webAppUrl = context.Properties["webAppUrl"].ToString();
}
Kuldeep Vasani
  • 310
  • 1
  • 3
  • 13