如何正确使用TestContext.Properties

How to properly make use of TestContext.Properties

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


我的内容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

但是,当我尝试在 TestClass 中实例化 TestContext 时,如下所示:

[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 class 不能是静态的

之后,您可以在任何单元测试中访问您想要的任何属性。这是一个例子:

[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);
    }
}

首先感谢@Peska 提供本回答中的代码


所以,最后我所做的是将以下代码添加到 class TestBase:

public class TestBase
{
    public TestContext TestContext { get; set; }

    public static int Colegio { get; set; }

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

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

装饰器[AssemblyInitialize]是必要的,[ClassInitialize][TestInitialize]是不行的,我相信是因为TestContext还没有被实例化。

之后,请确保您已通过转至 Test > Test Settings > Select Test Settings File 配置了 Test Settings File,该文件必须命名为 *.runsettings

至此,一切都应该设置好 TestContext.Properties 以从您的测试设置文件中读取

如本 Microsoft Document 中所述,您无需分配 TestContext 即可填充它。它会在每次 TestInitialize 调用之前自动分配。从那里,您可以通过 属性.

访问在 *.runsettings 中定义的属性

总而言之,只需添加一个类型为 属性 且名为 TestContext 的 TestContext,MS Test 将在运行时填充它。