gpt4 book ai didi

c# - 如何正确使用 TestContext.Properties

转载 作者:行者123 更新时间:2023-12-03 17:18:16 24 4
gpt4 key购买 nike

我需要访问 TestContext.Properties之前 TestMethod所以测试可以接收到正确的环境进行测试。

我的内容test.runsettings :

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<TestRunParameters>
<Parameter name="colegio" value="7" />
</TestRunParameters>



如您所见,该文件仅包含一个参数,名为 colegio (school)
这是 TestBase.cs的内容:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using InfraestructureSelenium.Helper;
using System.Collections.Generic;
using InfraestructureSelenium.Configuration.Enumerados;

namespace TestSelenium
{
[DeploymentItem("chromedriver.exe")]
[DeploymentItem("IEDriverServer.exe")]
[DeploymentItem("phantomjs.exe")]
[DeploymentItem("geckodriver.exe")]
[TestClass]
public class TestBase
{
protected TestBase()
{ }

public TestBase(int id = (int)ColegioEnum.ColegioDemoMovilidad2_Supervisor)
{
DiccionarioCompartido = new Dictionary<string, string>();
SeleniumHelper = new HelperSelenium(id, WebDriverSelector.ObtenerWebDriver());
}

public TestBase(HelperSelenium seleniumHelper, Dictionary<string, string> diccionarioCompartido = null)
{
SeleniumHelper = seleniumHelper;
}

public HelperSelenium SeleniumHelper { get; set; }

public static Dictionary<string, string> DiccionarioCompartido { get; set; }

public void CloseBrowser()
{
SeleniumHelper.Quit();
}

#region Configuración Test

[TestInitialize]
public void InitializeTest()
{

}

[TestCleanup]
public void TestCleanupTest()
{
SeleniumHelper.Quit();
}

#endregion
}
}

如您所见, public TestBase(int id = (int)ColegioEnum.ColegioDemoMovilidad2_Supervisor)接收一个参数,对应于 colegio我想测试的环境。如果没有 colegio作为参数传递,默认值为 ColegioEnum.ColegioDemoMovilidad2_Supervisor
但是,当我尝试实例化 TestContext 时在 TestClass , 像这样:

[TestClass]
public class GenerarBoletinDeClase : TestBase
{
public TestContext TestContext { get; set; }

private static TestContext _testContext;

[TestInitialize]
public static void SetupTests(TestContext testContext)
{
_testContext = testContext;
}

public GenerarBoletinDeClase() : base((int)TestContext.Properties["colegio"]) { }

出现以下错误: An object reference is required for the non-static field, method, or property
感谢任何帮助,因为我已经给了这个问题很多时间,但我无法继续前进。

最佳答案

你必须解决几件事:

  • TestInitialize 方法不能是静态的,它不应该有任何参数
  • 您将需要具有 ClassInitialize 属性和 TestContext 作为参数的静态方法
  • 您的测试类中的 TestContext 不能是静态的

  • 之后,您可以在任何单元测试中访问您想要的任何属性。下面是一个例子:
    [TestClass]
    public class GenerarBoletinDeClase
    {
    public TestContext TestContext { get; set; }

    public static int Colegio { get; set; }

    [ClassInitialize]
    public static void ClassInitialize(TestContext testContext)
    {
    Colegio = int.Parse(testContext.Properties["colegio"].ToString()); // colegio is equal 7 in here
    }

    [TestInitialize]
    public void TestInitialize()
    {
    int tempColegio = int.Parse(this.TestContext.Properties["colegio"].ToString()); // colegio is equal 7 in here
    }

    [TestMethod]
    public void TestMethod1()
    {
    int colegio = int.Parse(this.TestContext.Properties["colegio"].ToString()); // colegio is equal 7 in here as well

    Assert.AreEqual(7, Colegio);
    Assert.AreEqual(7, colegio);
    Assert.AreEqual(colegio, Colegio);
    }
    }

    关于c# - 如何正确使用 TestContext.Properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51364570/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com