简体   繁体   English

Coded UI Test中的TestContext TestRunParameters

[英]TestContext TestRunParameters in Coded UI Test

Following the procedures outlines here and here I was able to set a TestRunParameter and access it at run time in a Unit test. 按照这里这里的程序概述,我能够设置一个TestRunParameter并在运行时在单元测试中访问它。 I then repeated the exact same process in a coded UI test but am not able to access the Properties. 然后,我在编码的UI测试中重复了完全相同的过程,但无法访问属性。

My .runsettings file: 我的.runsettings文件:

<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? Coded UI测试是否需要其他配置才能访问这些运行时属性?

Edit: I notice that within the context of a Unit test, the TestContext is Microsoft.VisualStudio.TestPlatform.MSTestFramework.TestContextImplementation . 编辑:我注意到在单元测试的上下文中,TestContext是Microsoft.VisualStudio.TestPlatform.MSTestFramework.TestContextImplementation In the Coded UI test, the TestContext is Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapterContext . 在Coded UI测试中,TestContext是Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapterContext

The parameters defined in .runsettings TestRunParameter section can't be accessed in Coded UI test. 在Coded UI测试中无法访问.runsettings TestRunParameter部分中定义的参数。 When you debug the Coded UI test, you will find that TextContext.Properties contains some values, ResultsDirectory, AgentId and etc. 调试Coded UI测试时,您会发现TextContext.Properties包含一些值,ResultsDirectory,AgentId等。

However, parameter defined in the TestRunParameter section can't be found. 但是,找不到TestRunParameter部分中定义的参数。

在此输入图像描述

Instead of setting parameters in TestRunParameter section, you can create a .cvs or .xml file, and access the data via data-driven. 您可以创建.cvs或.xml文件,并通过数据驱动访问数据,而不是在TestRunParameter部分中设置参数。 Check this article for the details: 查看此文章了解详细信息:

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

Try Using it in ClassInitialize inplace of Test Method refer below code 尝试在ClassInitialize中使用它取代Test方法,请参考下面的代码

[ClassInitialize]
public static void TestClassinitialize(TestContext context)
{
    var webAppUrl = context.Properties["webAppUrl"].ToString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM