tags:

views:

30

answers:

2

Hi all, 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 initializing?

+1  A: 

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

Thomas Weller
A: 

(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)
    {
    }
}
Tom Brothers