ClassInitialize attribute in unit test based class not called

Phil picture Phil · Sep 6, 2011 · Viewed 19.8k times · Source

I added these method in a TestBase class :

[ClassInitialize]
public static void InitializBeforeAllTests()
{
}

But when I run in Debug an unit test Test1() :

[TestClass]
public class TestMapping : TestBase
{
    [TestMethod]
    public void Test1()
    {
    }

The TestBase.InitializBeforeAllTests() method is never called. Why?

Answer

Shaulian picture Shaulian · May 5, 2014

When declaring ClassInitialize attribute on a method, the method has to be static, public, void and should take a single parameter of type TestContext.

If you're having also other method with the AssemblyInitialize attribute on the same unit test, the test will run but will skip on all test methods and will go directly to AssemblyCleanup or just quit.

Try the example on ClassInitialize attribute in MSDN.