[英]How to access TestRunParameters within RunSettings file
.RunSettings
https://msdn.microsoft.com/en-us/library/jj635153.aspx我创建了一个.RunSettings
文件,其中包含一些类似于示例的参数:
<TestRunParameters>
<Parameter name="webAppUrl" value="http://localhost" />
<Parameter name="webAppUserName" value="Admin" />
<Parameter name="webAppPassword" value="Password" />
</TestRunParameters>
我计划为我们的每个环境都有一个.RunSettings
文件,其中包含适当的 URL 和凭据,用于在指定的 RunSettings 文件的环境上运行 CodedUI 测试。
我可以看到从命令行引用我可以运行的设置文件:
vstest.console myTestDll.dll /Settings:Local.RunSettings /Logger:trx
vstest.console myTestDll.dll /Settings:QA.RunSettings /Logger:trx
等等...
但我没有看到调用如何实际利用任何方式TestRunParameters
从codedUI测试中。
我想要做的是设置测试初始值设定项,使用TestRunParameters
来确定登录的位置以及使用的凭据。 像这样的东西:
[TestInitialize()]
public void MyTestInitialize()
{
// I'm unsure how to grab the RunSettings.TestRunParameters below
string entryUrl = ""; // TestRunParameters.webAppUrl
string userName = ""; // TestRunParameters.webAppUserName
string password = ""; // TestRunParameters.webAppPassword
LoginToPage(entryUrl, userName, password);
}
public void LoginToPage(string entryUrl, string userName, string password)
{
// Implementation
}
非常感谢有关如何引用TestRunParameters
信息!
编辑
/// <summary>
/// Summary description for CodedUITest1
/// </summary>
[CodedUITest]
public class CodedUITest1
{
public static string UserName = string.Empty;
[ClassInitialize]
public static void TestClassInitialize(TestContext context)
{
UserName = context.Properties["webAppUserName"].ToString();
Console.WriteLine(UserName);
}
[TestMethod]
public void CodedUITestMethod1()
{
this.UIMap.RecordedMethod1();
// To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
}
// Rest of the default class - TestContext instantiation, UI map instantiation, etc
}
我在运行时遇到的异常:
空引用异常
@williamfalconeruk 我已经更新了我的测试类,但我仍然遇到同样的错误,知道我做错了什么吗?
对于那些使用 Resharper 解决此问题的人,我发现了修复程序(无需禁用 Resharper):
转到 Visual Studio 顶部菜单 -> Resharper -> 选项
找到工具部分,展开“单元测试”
单击“MsTest”。 该复选框应启用,但下方的测试设置文件路径可能为空。 如果是,请单击浏览并选择要使用的运行设置文件。
单击保存,重建并尝试运行测试,参数现在应该可以工作了。
不知道为什么,但在使用 Resharper 时,仅从测试菜单中选择测试设置文件 -> 测试设置实际上不起作用,因此需要直接在 Resharper 选项中明确指向该文件。
我最近也遇到了这个问题,因为我们想摆脱遗留环境变量的使用。 下面提供的解决方案适合我们的需求,但可能有更好的解决方案......
事实证明,您可以在 Test 夹具的ClassInitialize
方法的TestContext
中访问这些。 有一个包含这些参数的Properties
字典,您可以在此处访问这些值:
[ClassInitialize]
public static void TestClassinitialize(TestContext context)
{
var webAppUrl = context.Properties["webAppUrl"].ToString();
//other settings etc..then use your test settings parameters here...
}
注意:这些是静态的,因此如果您需要访问它,您可能需要设置静态属性以在您的测试代码中访问。
建议的替代方法是使用数据驱动测试方法。 以下是有关数据驱动测试的一些基本信息,它们也可能有所帮助: https : //msdn.microsoft.com/en-us/library/ms182527.aspx
正如我所说,上述解决方案在基本层面上满足了我们的需求。
更新:请参阅下图以响应返回空值的测试设置...
这对我有用(VS2017-pro):
namespace TestApp.Test
{
[TestClass]
public class UnitTest1
{
// This enables the runner to set the TestContext. It gets updated for each test.
public TestContext TestContext { get; set; }
[TestMethod]
public void TestMethod1()
{
// Arrange
String expectedName = "TestMethod1";
String expectedUrl = "http://localhost";
// Act
String actualName = TestContext.TestName;
// The properties are read from the .runsettings file
String actualUrl = TestContext.Properties["webAppUrl"].ToString();
// Assert
Assert.AreEqual(expectedName, actualName);
Assert.AreEqual(expectedUrl, actualUrl);
}
[TestMethod]
public void TestMethod2()
{
// Arrange
String expectedName = "TestMethod2";
// Act
String actualName = TestContext.TestName;
// Assert
Assert.AreEqual(expectedName, actualName);
}
}
}
确保在此处选择您要使用的运行设置文件:测试 -> 测试设置。
禁用 Resharper 的另一种方法是启用 MSTest 支持并在 Resharper 选项对话框(->工具->单元测试->MsTest)上选择测试设置文件。
我也试图做这件事。 许多人可能知道,通过 MTM 运行测试会向 TestContext 公开一些附加属性,包括所使用的运行设置的名称。 我将此属性用作我们测试数据的各种“外键”,允许我们指定环境 URL 等,而无需对其进行硬编码或使用开箱即用的测试附带的令人难以置信的乏味的“数据驱动”工具。
当然,作为 BDT 或发布工作流的一部分执行测试时,除了 @kritner 正在尝试的 microsoft 在这里描述的内容之外,没有办法公开任何运行时属性。 但是,如果您阅读该链接的评论,您会发现您可以在这里推断出什么:
我们这些试图将 UI 或负载测试作为 CI 或 CD 工作流程的一部分来执行的人完全搞砸了。 即使在使用 MTM 中创建的某些测试配置(不是设置)执行计划/套件时,您也不会在 testContext 中获得任何其他属性。 @Adam 在运行 vs 调试时可能已经能够让它工作,但这可能只适用于单元测试。 通过 CodedUI,我一直无法在没有获得 NullReferenceException 的情况下检索属性。 这是我用来调查的 janky 代码的示例:
if (testContextInstance.Properties["__Tfs_TestConfigurationName__"] != null) //Exposed when run through MTM
{
TFSTestConfigurationName = testContextInstance.Properties["__Tfs_TestConfigurationName__"].ToString();
}
else TFSTestConfigurationName = "Local"; //Local
var configName = testContextInstance.Properties["configurationName"] ?? "No Config Found";
Trace.WriteLine("Property: " + configName);
和我的 .runsettings 文件的 XML:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<!-- Parameters used by tests at runtime. These are required as a substitute for TFS/MTM test settings.-->
<!-- File instructions: https://msdn.microsoft.com/en-us/library/jj635153.aspx#example -->
<!-- TFS instructions: https://blogs.msdn.microsoft.com/visualstudioalm/2015/09/04/supplying-run-time-parameters-to-tests/ -->
<TestRunParameters>
<Parameter name="configurationName" value="Local" />
</TestRunParameters>
</RunSettings>
以及 BDT 工作流程生成的 .trx 的摘录:
Property: No Config Found
对于 NullReferenceException 问题:
我最近也面临同样的问题,解决方案是使用 Visual Studio 2013 的最新更新。 现在最新的更新是更新 5。我不确定哪个特定更新解决了这个问题。 我应用了更新 5 并且能够成功访问 ClassInitialize 方法中的 TestRunParameters。
你可以找到更新@ https://support.microsoft.com/en-us/kb/2829760所以我有两台机器,一台机器工作正常,另一台机器出现异常。 我查了一下,唯一的区别是VS的Update; 应用它并解决了问题。 :)
我能够通过禁用 Resharper 为单元测试解决这个问题。 希望我能对编码的 UI 测试说同样的话。
使用 NUnit 3,我能够在 runsettings 文件中找到属性,使用
TestContext.Parameters
所以在这种情况下,它将是:
string entryUrl = TestContext.Parameters["webAppUrl"];
string username = TestContext.Parameters["webAppUserName"];
string password = TestContext.Parameters["webAppPassword"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.