如何正确利用TestContext.Properties

时间:2018-07-16 14:50:35

标签: c# selenium mstest

我需要在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

感谢我的帮助,因为我已经给了这个问题很多时间,而且我无法继续前进。

2 个答案:

答案 0 :(得分:1)

您必须修复一些问题:

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

答案 1 :(得分:1)

首先,感谢@Peska在此答案https://stackoverflow.com/a/51367231/5364231中提供了代码


因此,最后我要做的是将以下代码添加到类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 Settings File配置了Test > Test Settings > Select Test Settings File,该文件必须命名为*.runsettings

因此,应该为TestContext.Properties进行所有设置,以从您的测试设置文件中读取