Reputation: 123
I have a base class ScriptBase
which has a virtual function called MyTestInitialize()
. When I call MyTestInitialize()
from derived class, then the value of testContextInstance
is null
.
Is there any solution for this? Please help as I am new to Automation Testing.
Thanks in Advance
[CodedUITest]
public class ScriptsBase
{
public ScriptsBase()
{
}
private static TestContext bingTestContext;
public static TestContext BingTestContext
{
get { return ScriptsBase.bingTestContext; }
set { ScriptsBase.bingTestContext = value;}
}
#region TestInitialize
//Use TestInitialize to run code before running each test
[TestInitialize()]
public virtual void MyTestInitialize()
{
Browser.CloseAllBrowsers();
BingTestContext = testContextInstance;
}
#endregion
#region TestCleanup
//Use TestCleanup to run code after each test has run
[TestCleanup()]
public virtual void MyTestCleanup()
{
PPI.HomePage = new HomePageUI();
Browser.CloseAllBrowsers();
}
#endregion
#region TestContext
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
private TestContext testContextInstance;
#endregion
}
public class DestinationMasterTestScripts : ScriptsBase
{
public DestinationMasterTestScripts()
{
}
[TestInitialize()]
public override void MyTestInitialize()
{
Console.WriteLine("Initialize");
base.MyTestInitialize();
}
}
Upvotes: 12
Views: 10892
Reputation: 765
Not sure if something has changed in the 6 years since this question was posted, but this works for me almost as-is. Add [TestClass] to the derived class and the TestContext gets set up just fine. There's also no need for BingTestContext at all. Just use this.TestContext from derived classes.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
public class ScriptsBase
{
public static TestContext BingTestContext { get; set; }
public TestContext TestContext { get; set; }
[TestInitialize]
public virtual void MyTestInitialize()
{
BingTestContext = this.TestContext;
}
[TestCleanup]
public virtual void MyTestCleanup()
{
}
}
[TestClass]
public class DestinationMasterTestScripts : ScriptsBase
{
[TestInitialize]
public override void MyTestInitialize()
{
base.MyTestInitialize();
}
[TestMethod]
public void Foo()
{
Console.WriteLine(this.TestContext);
}
}
Upvotes: 1
Reputation: 1
See if this helps, I find it working when I set base class TestContext
with derived class one.
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
base.TestContext = value;
testContextInstance = value;
}
}
private TestContext testContextInstance;
Upvotes: 0
Reputation: 1
You should use equal classes for Assert
and TestContext
like:
using Assert = NUnit.Framework.Assert;
using TestContext = NUnit.Framework.TestContext;
Upvotes: -1
Reputation: 10784
Another option is to declare the TestContext as abstract in your base class
public abstract TestContext TestContext { get; set; }
And override it in your most derived concrete class(es)
public override TestContext TestContext { get; set; }
Upvotes: 5
Reputation: 11287
Try creating a ClassInitialize method:
private static TestContext bingTestContext
[ClassInitialize]
public static void ClassInit(TestContext con)
{
bingTestContext = con;
}
Upvotes: 7