如何在RunSettings文件中访问TestRunParameters

Kri*_*ner 35 c# asp.net coded-ui-tests vs-unit-testing-framework 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>
Run Code Online (Sandbox Code Playgroud)

我计划.RunSettings为每个环境提供一个文件,其中包含适当的URL和凭据,用于在指定的RunSettings文件环境中运行CodedUI测试.

我可以看到从命令行引用我可以运行的设置文件:

vstest.console myTestDll.dll /Settings:Local.RunSettings /Logger:trx
vstest.console myTestDll.dll /Settings:QA.RunSettings /Logger:trx
Run Code Online (Sandbox Code Playgroud)

等等...

但我没有看到任何方式来调用如何TestRunParameters在codedUI测试中实际使用from.

我想要做的是设置测试初始化​​程序,使用它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
}
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

运行时遇到的异常:

NullReference异常

在此输入图像描述

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

wil*_*ruk 22

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

事实证明,您可以在Test fixture 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...
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

Visual Studio中的测试设置

  • 我发现只有当您使用VS Test Explorer - 运行测试时才能使用它,而不是在您进行调试测试或使用R#Test Runner时.我的设置是VS2015.Update 2.使用R#Ultimate 10.0.1 (6认同)
  • 为了完整起见,还有另一个地方可以设置testsettings文件.解决方案资源管理器中".testsetting"文件的上下文菜单具有可选的"活动Web和负载测试设置"条目.这个仅适用于Web和负载测试.我不知道为什么Visual Studio有两种截然不同的方式来指定这些文件. (2认同)

sta*_*uxe 20

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

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

  2. 找到工具部分,展开"单元测试"

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

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

不确定为什么,但只是从测试菜单中选择测试设置文件 - >测试设置在使用Resharper时实际上不起作用,因此需要在Resharper选项中直接显式指向此文件.

  • 我希望我可以加倍喜欢这个答案.在我读到这篇文章之前,我一直在抨击这个问题.哇这对于VS/Resharper来说对用户来说是一个令人难以置信的蹩脚的事情! (3认同)

Pau*_*515 5

这对我有用(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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

确保在此处选择您要使用的运行设置文件:测试 -> 测试设置。