如何在RunSettings文件中访问TestRunParameters

通过https://msdn.microsoft.com/en-us/library/jj635153.aspx阅读我创建了一个.RunSettings文件,其中包含一些类似于示例的参数:

       

我计划为每个环境提供一个.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 description for CodedUITest1 ///  [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 } 

运行时遇到的exception:

NullReferenceexception

在此处输入图像描述

@williamfalconeruk我已经更新了我的测试类,但是我仍然得到同样的错误,知道我做错了什么?

最近我也遇到过这个问题,因为我们希望摆脱遗留环境变量的使用。 下面提供的解决方案适合我们的需求,但可能有更好的解决方案……

事实certificate,您可以在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... } 

注意 :这些是静态的,因此如果您需要访问它,您可能需要在Test代码中设置要访问的静态属性。

另一种建议是使用数据驱动测试方法。 以下是数据驱动测试的一些基本信息,这些信息也可能有所帮助: https : //msdn.microsoft.com/en-us/library/ms182527.aspx

正如我所说,上述解决方案适合我们的基本需求。

更新:请参阅下面的图片以响应返回null的测试设置…

Visual Studio中的测试设置

对于那些使用Resharper这个问题的人,我发现了修复(不需要禁用Resharper):

  1. 转到Visual Studio顶部菜单 – > Resharper – >选项

  2. 找到工具部分,展开“unit testing”

  3. 点击“MsTest”。 该复选框应该已启用,但下面的测试设置文件路径可能为空。 如果是,请单击“浏览”并选择要使用的runsettings文件。

  4. 单击“保存”,“重建”并尝试运行测试,参数现在应该可以正常工作。

不确定为什么,但只是从测试菜单中选择测试设置文件 – >测试设置在使用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之外,在执行作为BDT或发布工作流的一部分的测试时,没有办法公开任何运行时属性。 但是,如果您阅读该链接的评论,您将发现您可以在此处推断出的内容:

  • 您需要使用VS 2013 R5或VS 2015才能使用此解决方案
  • 它只适用于unit testing!

我们这些试图将CI或负载测试作为CI或CD工作流程的一部分执行的人完全搞砸了。 即使在使用MTM中创建的某些测试配置 (而非设置)执行计划/套件时,您也无法在testContext中获得任何其他属性。 @Adam可能已经能够在运行与调试时使其工作,但这可能只适用于unit testing。 通过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:

          

并摘录了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测试说同样的话。

为什么不使用应用程序设置?

的AppSettings

然后你可以在任何地方阅读它们

 var yesICan= Properties.Settings.Default.IToldYou; 

你可以从他们创建属性,你可以做很多事情。

 public string URL_WEBOFFICE { get { return Properties.Settings.Default.WEBOFFICE_URL.Replace("***", Properties.Settings.Default.SiteName); } }