Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在运行时访问测试设置文件的属性_C#_Visual Studio_Unit Testing_Vs Unit Testing Framework - Fatal编程技术网

C# 在运行时访问测试设置文件的属性

C# 在运行时访问测试设置文件的属性,c#,visual-studio,unit-testing,vs-unit-testing-framework,C#,Visual Studio,Unit Testing,Vs Unit Testing Framework,我有一个使用MSTest的测试项目。我有一个testsettings文件,该文件中有一个属性。如下 <?xml version="1.0" encoding="UTF-8"?> <TestSettings name="local" id="77572268-dd99-4f8c-a660-f5c8c1eec977" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">

我有一个使用MSTest的测试项目。我有一个testsettings文件,该文件中有一个属性。如下

<?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>
它给出了以下例外

中发生“System.NullReferenceException”类型的异常 TestAutomation.dll,但未在用户代码中处理

其他信息:对象引用未设置为 反对


这不是关于System.NullReferenceException的,而是关于如何在运行时访问测试设置文件中的属性。所以这个问题不是重复的。

我怀疑您没有正确配置.runsettings文件

按照此链接正确配置“设置文件”部分


或者,您也可以尝试“TestRunParameters”部分来实现此功能。

您访问属性的方式不正确。您需要改用runsettings文件

@mohithrivastava的可能副本这不是一个副本,这是一个单独的场景我需要在测试设置文件中添加属性,因为我已经在使用它来指定部署项。因此,我可以同时使用运行设置和测试设置一次。我正在使用命令行通过传递参数来执行此测试。我认为您可以。您可以指定指向测试设置的运行设置。
   [ClassInitialize]
    public static void TestClassInitialize(TestContext context)
    {
        var sad = context.Properties["AAA"].ToString();
    }