Unit Tests inheritance
How do i make Base class with [TestClass()], where i will do MyClassInitialize(), and after that, i will just make my another Test classes just like that - MyNewTest : BaseTest and there will no initiali开发者_运维知识库zing?
(using MSTest)
The ClassInitialize won’t work on a base class. It seems that this attribute is searched for only on the executed test class. However, you can call the base class explicitly.
Here is an example:
[TestClass]
public class MyTestClass : TestBase
{
[TestMethod]
public void MyTestMethod()
{
System.Diagnostics.Debug.WriteLine("MyTestMethod");
}
[ClassInitialize]
public new static void MyClassInitialize(TestContext context)
{
TestBase.MyClassInitialize(context);
}
}
[TestClass]
public abstract class TestBase
{
public TestContext TestContext { get; set; }
public static void MyClassInitialize(TestContext context)
{
System.Diagnostics.Debug.WriteLine("MyClassInitialize");
}
[AssemblyInitialize]
public static void AssemblyInit(TestContext context)
{
}
}
In NUnit/MbUnit you simply put the Initalize/Cleanup methods with the respective attributes in the base class, then inherit from it.
I haven't tried this yet with MSTest, but I wouldn't recommend this framework anyway.
Thomas
精彩评论