56

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?

1

7 Answers 7

18

When declaring ClassInitialize attribute on a method, the method has to be static, public, void (or Task, if async) 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.

Sign up to request clarification or add additional context in comments.

Comments

13

i know this is a very old question, but its the first to popup in google search when looking for a similar problem, anyhow, here is an update for the answer:

 [ClassInitialize(InheritanceBehavior.BeforeEachDerivedClass)]
 public static void YOUR_INIT_METHOD_NAME(TestContext context)

Note: you need MSTest.TestFramework-Version 2.0.0 package or newer for this to work.

1 Comment

note: if 2 classes inherit the same base class - the [ClassInitialize..] method is invoked 2 times. Unlike for example a static constructor which would only get invoked once. Having said that - you can still use the internal static variable to check if the class was previously initialized.
10

You can setup an assembly initialize method in your base class. Not quite the same as ClassInitialize, but it's a viable option. Source:The Workaround mentioned here.

[TestClass]
public abstract class TestBase
{
    [AssemblyInitializeAttribute]
    public static void Initialize(TestContext context)
    {
        // put your initialize code here
    }
}

You can also add a Cleanup method:

[AssemblyCleanup]
public static void Cleanup()
{
   //clean up stuff here
}

Comments

9

for whatever reason, the unit test framework's UnitTestExecuter only allows one ClassInitialize and one ClassCleanup method to be defined per test class... unlike TestInitialize and TestCleanup methods, which get called in both the derived and base test class...

Comments

0

The MS link is not working anymore. Anyway, one way to work around this issue is to simply move your initialization code into the constructor of the base class. This will ensure that it gets called from any descendant classes whenever they are instantiated.

[TestClass]
public class TestBase
{
    public TestBase()
    {
        // Initialization Code
    }
}

[TestClass]
public class TestMapping : TestBase
{
    [TestMethod]
    public void Test1()
    {
        // At this point the base constructor should have been called
    }
}

1 Comment

No, you get an entire new instance of the test class for every TestMethod. So the Ctor will be called before each test (like TestInitialize) which is not what the OP wants.
0

In my case, I had the[Ignore] attribute applied (test is ran manually)
This caused [AssemblyInitialize] to never be called

If I removed the [Ignore] attribute [AssemblyInitialize] was called as expected.

Oddly, [AssemblyCleanup] is still called with or without the [Ignore] applied to my test

Comments

0

In my case [ClassInitialize] method was never called due to the fact that I haven't passed TestContext as a parameter. Even if it's optional I guess you have to pass it every time even though you don't use it in the method.

So method signature

[ClassInitialize(InheritanceBehavior.None)]
public static void OnClassInitialize(TestContext context)
{
    // Log in to the system
}

worked for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.