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 TestContext
in 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.
TestContext encapsulates the context in which a test is executed, agnostic of the actual testing framework in use.
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.
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 .
You have to fix few things:
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);
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With