-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,930
  • 22
  • 75
  • 117
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Mohit S Nov 19 '15 at 04:48
  • @MohitShrivastava This is not a duplicate, this is a separate scenario – tarzanbappa Nov 19 '15 at 04:49

3 Answers3

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
  • 63
  • 1
  • 5
  • 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,947
  • 8
  • 38
  • 62
0

You were close but you dont need the ending tag of the property tag, here is an example:

<Properties>
    <Property name="test" value="testValue"/>
</Properties>

Once that is done you can use the same code to access the data: context.Properties["AAA"].ToString() debug window showing the code above

Bhagyesh
  • 700
  • 10
  • 31