Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly make use of TestContext.Properties

I need to access TestContext.Properties before a TestMethod so the test can receive the correct environment to test.


The contents of my test.runsettings:

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

As you can see, the file only contains one parameter, called colegio (school)


This are the contents of 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
        }
    }

As you can see, public TestBase(int id = (int)ColegioEnum.ColegioDemoMovilidad2_Supervisor) receives an argument, which corresponds to the colegio environment I want to test. If no colegio is passed as an argument, the default one will be ColegioEnum.ColegioDemoMovilidad2_Supervisor

However, when I try to instantiate TestContextin a TestClass, like this:

[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"]) { }

The following error appears: An object reference is required for the non-static field, method, or property

Any help is appreciated as I've given this problem a lot of time and I couldn't progress any further.

like image 724
elgato Avatar asked Jul 16 '18 14:07

elgato


People also ask

What is the use to TestContext?

TestContext encapsulates the context in which a test is executed, agnostic of the actual testing framework in use.

What is TestContext Mstest?

Each NUnit test runs in an execution context, which includes information about the environment as well as the test itself. The TestContext class allows tests to access certain information about the execution context.

How do I create a Runsettings file?

Create a run settings file and customize itIn Solution Explorer, on the shortcut menu of your solution, choose Add > New Item, and select XML File. Save the file with a name such as test. runsettings. The file name doesn't matter, as long as you use the extension .


2 Answers

You have to fix few things:

  • TestInitialize method cannot be static and it shouldn't have any parameters
  • You will need static method with ClassInitialize attribute and TestContext as parameter
  • TestContext in your test class cannot be static

After that, you can access whatever properties you want in any unit tests. Here is an example:

[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);
    }
}
like image 138
Peska Avatar answered Sep 25 '22 15:09

Peska


First of all, thanks to @Peska for providing the code in this answer https://stackoverflow.com/a/51367231/5364231


So, finally what I did was add the following code to the 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>();
    }

The decorator [AssemblyInitialize] is necessary, [ClassInitialize] and [TestInitialize] will not work, I believe because the TestContext has not been instantiated yet.

After that, make sure that you have configured a Test Settings File by going to Test > Test Settings > Select Test Settings File, the file must be named *.runsettings

With that, everything should be set up for TestContext.Properties to read from your test settings file

like image 37
elgato Avatar answered Sep 24 '22 15:09

elgato