通过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异常
@williamfalconeruk我已经更新了我的测试类,但我仍然得到同样的错误,知道我做错了什么?
最近我也遇到过这种情况,因为我们希望摆脱遗留环境变量的使用。下面提供的解决方案适合我们的需求,但可能有更好的解决方案......
事实证明,您可以在测试夹具的TestContext
方法的ClassInitialize
中访问这些。有一个Properties
字典有这些参数,你可以在这里访问值:
[ClassInitialize]
public static void TestClassinitialize(TestContext context)
{
var webAppUrl = context.Properties["webAppUrl"].ToString();
//other settings etc..then use your test settings parameters here...
}
注意:这些是静态的,因此如果您需要访问它,您可能需要设置静态属性以在Test代码中访问。
另一种建议是使用数据驱动测试方法。以下是数据驱动测试的一些基本信息,这些信息也可能有所帮助:https://msdn.microsoft.com/en-us/library/ms182527.aspx
正如我所说,上述解决方案适合我们的基本需求。
更新:请参阅下面的图片以响应返回null的测试设置...
对于那些使用Resharper这个问题的人,我发现了修复(不需要禁用Resharper):
不确定为什么,但只是从测试菜单中选择测试设置文件 - >测试设置在使用Resharper时实际上不起作用,因此需要在Resharper选项中直接显式指向此文件。
禁用Resharper的替代方法是启用MSTest支持并在Resharper Options对话框中选择测试设置文件( - > Tools-> Unit Testing-> MsTest)。
对于NullReferenceException问题:
我最近也面临同样的问题,它的解决方案是拥有Visual Studio 2013的最新更新。现在最新的更新是Update 5.我不确定哪个特定的更新修复了这个问题。我应用了Update 5,并且能够成功访问ClassInitialize方法中的TestRunParameters。
你可以找到更新@ https://support.microsoft.com/en-us/kb/2829760所以我有两台机器,一台机器都工作正常,另一台我得到了例外。我调查过,唯一的区别是VS的更新;应用它,这解决了问题。 :)
我也试图做到这一点。许多人可能知道,通过MTM运行测试会向TestContext公开一些其他属性,包括使用的运行设置的名称。我使用这个属性作为我们的测试数据的“外键”,允许我们指定环境URL等,而无需硬编码或使用开箱即用测试带来的令人难以置信的“数据驱动”工具。
当然,除了@kritner正在尝试微软描述HERE之外,在执行测试时,除了@kritner正在尝试将BDT或发布工作流作为其中一部分时,也无法公开任何运行时属性。但是,如果您阅读该链接的评论,您将发现您可以在此处推断出的内容:
我们这些试图将CI或负载测试作为CI或CD工作流程的一部分执行的人完全搞砸了。即使在使用MTM中创建的某些测试配置(而非设置)执行计划/套件时,您也无法在testContext中获得任何其他属性。运行与调试时,@ Adam可能已经能够使用它,但这可能只适用于单元测试。通过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
这对我有用(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);
}
}
}
确保选择要使用的runsettings文件,此处为:Test - > Test Settings。
我能够通过禁用Resharper来解决这个问题。希望我能为Coded UI测试说同样的话。