通过阅读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等等。
但我看不到任何方法来说明如何在codedUI测试中实际利用TestRunParameters。
我想要做的是设置测试初始化器,它使用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
}我在运行时得到的异常:
NullReference异常

我已经像上面一样更新了我的测试类,但是我仍然得到同样的错误,你知道我做错了什么吗?
发布于 2016-07-04 17:05:11
对于那些使用Resharper解决这个问题的人,我发现了修复(不需要禁用Resharper):
不知道为什么,但简单地从测试菜单中选择测试设置文件->测试设置在使用Resharper时实际上并不起作用,因此需要在Resharper选项中直接明确指向此文件。
发布于 2015-08-05 19:07:48
我最近也遇到了这一点,因为我们想要摆脱遗留环境变量的使用。下面提供的解决方案适合我们的需求,但也可能有更好的解决方案...
事实证明,您可以在Test fixture的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
正如我所说的,上面的解决方案在基本层面上满足了我们的需求。
更新:请参见下图,以响应返回null的测试设置...

发布于 2017-12-27 17:27:31
这对我很有效(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);
}
}
}确保选择要使用的运行设置文件,此处为:测试->测试设置。
https://stackoverflow.com/questions/31707415
复制相似问题