Using a Base Class for your Unit Test Classes

Overview:

This post will go over some simple but effective ways to setup common initialization and cleanup for Unit Tests on a larger scale than [ClassInitialize] and [TestInitialize] methods can provide for.

First, to establish a common starting point I’ll go over some of the basics, if you’re already familiar with using the ClassInitialize/Cleanup and TestInitialize/Cleanup attributes in unit tests you may wish to skip this next part.

 

Background:

When you have some setup and cleanup code that needs to run for several unit tests typically you would put them in the same Test Class and use a combination of Class Initialize/Cleanup and Test Initialize/Cleanup methods.

A simple start would be something like this:

 

 [TestClass]
public class TestClass1
{
    [TestInitialize]
    public void TestInit()
    {
        Console.WriteLine("TestClass1.TestInit()");
    }

    [TestMethod]
    public void TestMethod1()
    {
        Console.WriteLine("TestClass1.TestMethod1()");
    }

    [TestMethod]
    public void TestMethod2()
    {
        Console.WriteLine("TestClass1.TestMethod2()");
    }

    [TestCleanup]
    public void TestCleanup()
    {
        Console.WriteLine("TestClass1.TestCleanup()");
    }
}

If you run both tests here the output will be:

TestClass1.TestInit()

TestClass1.TestMethod1()

TestClass1.TestCleanup()

and

TestClass1.TestInit()

TestClass1.TestMethod2()

TestClass1.TestCleanup()

The methods marked with the attributes [TestInitialize] and [TestCleanup] run before and after each test in that class. If instead you’d like to only run the initialization code once before all tests (not each individual test) you could use [ClassInitialize] and [ClassCleanup] instead, or you can also use them in combination.

 

Going Beyond Local ClassInitialize and TestInitialize

What can I do if I have a large project, with dozens or even hundreds of unit test methods spread across several classes and you want to share some setup or cleanup code between those tests?

 

One approach would be to create some initialize and cleanup helper methods in a separate class and call those methods from each of your individual test classes initialize and cleanup methods.

Another approach, the one I personally prefer, is to create a base class for your test classes. For example:

 

 [TestClass]
public class TestBase
{
    [TestInitialize]
    public void BaseTestInit()
    {
        Log.AppendLine("TestBase.BaseTestInit()");
    }

    [TestCleanup]
    public void BaseTestCleanup()
    {
        Console.WriteLine(Log.ToString());
    }

    public static StringBuilder Log
    {
        get
        {
            if (s_log == null)
            {
                s_log = new StringBuilder();
            }

            return s_log;
        }
    }

    static StringBuilder s_log;
}


[TestClass]
public class TestClass1 : TestBase
{
    [ClassInitialize]
    public static void ClassInit(TestContext testContext)
    {
        Log.AppendLine("TestClass1.ClassInit()");
    }

    [TestInitialize]
    public void TestInit()
    {
        Log.AppendLine("TestClass1.TestInit()");
    }

    [TestMethod]
    public void TestMethod1()
    {
        Log.AppendLine("TestClass1.TestMethod1()");
    }
} 

Notice that the base class “TestBase” is also using the [TestClass] attribute, although we won’t be putting any test methods in this class. This allows the use of the [TestInitialize] and [TestCleanup] attributes within our base class. If you ran the tests in TestClass1 you would see the following output:

 

TestClass1.ClassInit()

TestBase.BaseTestInit()

TestClass1.TestInit()

TestClass1.TestMethod1()

 

The [ClassInitialize] will always run first, it’s static and will be invoked by the unit test engine before instantiating the test class. Next we see that the Test Initializer in the base class is called, followed by the Test Initializer in the test class itself, and lastly the test method is executed.

 

 

How can I create an initialization method that will run before any class initialization methods in my test project?

 

Building on the common base class approach described above you could simply add a static constructor to your base class and either perform the initialization there or call the method that will perform the desired initialization. The resulting base class might look like this:

 

 [TestClass]
public class TestBase
{
    static TestBase()
    {
        s_log = new StringBuilder();
        Log.AppendLine("TestBase.ctor()");
    }

    [TestInitialize]
    public void BaseTestInit()
    {
        Log.AppendLine("TestBase.BaseTestInit()");
    }

    [TestCleanup]
    public void BaseTestCleanup()
    {
        Console.WriteLine(Log.ToString());
    }

    public static StringBuilder Log
    {
        get { return s_log; }
    }

    static StringBuilder s_log;
} 

 

Will the same approach that was used for [TestInitialize] work with [ClassInitialize] in a base class?

 

Not exactly, if you create a [ClassInitialize] attributed method in the base class it won’t ever get called unless you explicitly call it at the beginning of your derived test classes ClassInitialize method; which of course is nowhere near as nice as the above approach.

 

If you really wanted this functionality you could hook the method calls using reflection and set things up that way, but that’s beyond the scope of this post.

 

Setting up the relationship in the reverse would be much easier, but is of questionable value. By reverse order I mean that it would be easier to create a method that resided in the base class and was called once per test class but it would be called after the derived classes ClassInitialize method.

 

The only viable option that I can come up for achieving an inheritable class initialization approach would be to ditch the ClassInitialize mechanism altogether and go back to good old fashioned class constructors.

 

Example:

 

     [TestClass]
    public class TestBase
    {
        static TestBase()
        {
            s_log = new StringBuilder();
            Log.AppendLine("TestBase.TestBase() <-- acts as ClassInitialize in base");
        }
        
        [TestInitialize]
        public void BaseTestInit()
        {
            Log.AppendLine("TestBase.BaseTestInit()");
        }

        [TestCleanup]
        public void BaseTestCleanup()
        {
            Console.WriteLine(Log.ToString());
        }

        public static StringBuilder Log
        {
            get { return s_log; }
        }

        static StringBuilder s_log;
    }

     [TestClass]
    public class TestClass1 : TestBase
    {
        static TestClass1() //Replaces ClassInitialize method
        {
            Log.AppendLine("TestClass1.TestClass1() <-- acts as ClassInitialize in derived");
        }

        [TestInitialize]
        public void TestInit()
        {
            Log.AppendLine("TestClass1.TestInit()");
        }

        [TestMethod]
        public void TestMethod1()
        {
            Log.AppendLine("TestClass1.TestMethod1()");
        }

        [TestMethod]
        public void TestMethod2()
        {
            Log.AppendLine("TestClass1.TestMethod2()");
        }
    } 
 Running TestMethod1 in the derived class produces the following output:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 

TestBase.TestBase() <-- acts as ClassInitialize in base

TestClass1.TestClass1() <-- acts as ClassInitialize in derived

TestBase.BaseTestInit()

TestClass1.TestInit()

TestClass1.TestMethod1()

TestBase.BaseTestInit()

TestClass1.TestInit()

TestClass1.TestMethod2()

Comments

  • Anonymous
    May 16, 2008
    PingBack from http://blogs.msdn.com/densto/archive/2008/05/16/using-a-base-class-for-your-unit-test-classes.aspx

  • Anonymous
    March 04, 2010
    I don't think that the last part of your post is accurate.  MSTest creates your test fixture once for each test and hence it does not double for ClassInitialize. TestDriven.Net DOES support inheritance of ClassInitialize but it does not support AssemblyInitialize.  In addition, if you're using MSTest you're probably looking to it for the TFS integration so you have to make sure that works too.

  • Anonymous
    March 17, 2010
    Good catch George, I corrected the last part so that it works as described.  The constructors should of course be static in order to work as the static ClassInitialize would.  

  • Anonymous
    April 01, 2011
    Your approach of putting common initialization code in a base class is perfect, with one caveat. If you use the nifty new TestCategory attribute, your inherited code won't get run if you specify one or more categories, unless you give the base class its own category (i.e., "always"), and remember to always specify the "always" category.

  • Anonymous
    April 01, 2011
    After more research, things seem a little more complicated. TestInitialize and TestCleanup seem to work from the base class, regardless of categories, even if the base class isn't marked as a TestClass. ClassInitialize, ClassCleanup, and AssemblyInitialize, however, only seem to work from the derived class, regardless of categories, even if the base class is marked as a TestClass. This is Visual Studio 2010 SP1 targeted at .NET 4.0.

  • Anonymous
    April 18, 2012
    The comment has been removed

  • Anonymous
    January 31, 2014
    Good info, covers almost everything... How would you do the equivalent of [ClassCleanup]? I've tried finalizers but they don't seem to do what I'm expecting...