51

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?

Dennis van Opstal
  • 1,294
  • 1
  • 22
  • 44
Phil
  • 1,035
  • 2
  • 10
  • 17
  • Use the static constructor on the base class. Similar question to this one: https://stackoverflow.com/questions/15944504/mstest-classinitialize-and-inheritance/56634082#56634082 – Mladen B. Jun 17 '19 at 15:08

7 Answers7

17

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.

rory.ap
  • 34,009
  • 10
  • 83
  • 174
Shaulian
  • 411
  • 5
  • 8
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
}
James Lawruk
  • 30,112
  • 19
  • 130
  • 137
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...

Paul Killick
  • 427
  • 6
  • 9
7

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.

YazX
  • 444
  • 4
  • 12
  • 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. – Alex Apr 10 '22 at 17:38
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
    }
}
  • 2
    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. – cid Apr 06 '17 at 09:19
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

Branden Barber
  • 1,717
  • 1
  • 17
  • 15
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.

Nishan
  • 3,644
  • 1
  • 32
  • 41