Test ClassInitialize and ClassCleanup Inheritance

When you have a framework of inherited test classes the following does not work:

 

[TestClass]

public class TestBase()

{

      [ClassInitialize]

      public static void TestBaseClassInitialize(TestContext context)

      {

          //never gets called when running inherited test.

      }

      [ClassCleanup]

     public static void TestBaseClassCleanup()

    {

       //never gets called when running inherited test.

     }

}

 

 

Here’s the solution

[TestClass]

public class TestBase()

{

static TestBase()

{

     //will be called from inherited place at the beginning of first test

    //also register a finalizer here:

     AppDomain.CurrentDomain.DomainUnload += CurrentDomain_DomainUnload

  

}

static CurrentDomain_DomainUnload(object sender, EventArgs e)

{

    //event will fire at the end of all tests.

}

}

 

Advertisements

About vijayvepa

I'm a software consultant for Software Specialists Inc. currently working at Philips Respironics, Pittsburgh

One comment

  1. Thank you! This works perfectly!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: