-3

I have a test project which uses MSTest. And I have a testsettings file and have a properties in that file. as below.

<?xml version="1.0" encoding="UTF-8"?>
<TestSettings name="local" id="77572268-dd99-4f8c-a660-f5c8c1eec977" 
              xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
  <Description>These are default test settings for a local test run.</Description>

  <Execution>
    <TestTypeSpecific>
      <UnitTestRunConfig testTypeId="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b">
        <AssemblyResolution>
          <TestDirectory useLoadContext="true" />
        </AssemblyResolution>
      </UnitTestRunConfig>
    </TestTypeSpecific>
    <AgentRule name="Execution Agents">
    </AgentRule>
  </Execution>
  <Properties >
    <Property name="AAA" value="val1"></Property>
    <Property name="BBB" value="val2"></Property>
  </Properties>
</TestSettings>

But how can I access these properties in testsettings file values by name in runtime. How can I do that?

This is what currently I'am trying..

   [ClassInitialize]
    public static void TestClassInitialize(TestContext context)
    {
        var sad = context.Properties["AAA"].ToString();
    }

And it gives following exception

An exception of type 'System.NullReferenceException' occurred in TestAutomation.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

And this is not about the System.NullReferenceException and this is about how to access a property in a Test settings file in runtime. So this question is not a duplicate.

tarzanbappa
  • 4,629
  • 13
  • 66
  • 99

2 Answers2

0

I suspect you have not properly configured your .runsettings file.

Follow this link .runsettings file configuration and configure the "SettingsFile" section properly.

Alternatively you can also try "TestRunParameters" section to get this working.

Humayun Khan
  • 11
  • 1
  • 3
  • I need to add the property in Test settings file because I'm already using it to specify deployment Items. So can I use both run settings & test settings once. I'm executing this test using command line by passing parameters – tarzanbappa Nov 19 '15 at 05:11
  • I think you can. You can specify the runsettings which points to your test settings. – Humayun Khan Nov 19 '15 at 10:42
0

The way you are accessing the properties is not correct. You need to use runsettings file instead.

Timothy Rajan
  • 1,784
  • 6
  • 33
  • 57