Pre-Build and Post-Build events are powerful yet underused feature of Visual Studio. In this article I will explain how to use the Post-Build event to run your unit tests.

Introduction:

Pre-Build and Post-Build events are powerful yet underused feature of Visual Studio. In this article I will explain how to use the Post-Build event to run your unit tests.

Why Not Use Nant and Build Automation?

Ideally, Nant scripts and automated builds are much better solution then using the Pre/Post Build options. But if you are working on a small project and don’t want to deal with writing Nant scripts then you will find this solution to be useful.

Project Structure:

The project structure is fairly simple. We have three projects added to a single solution. Here is the list of the projects:

1) MyClassLibrary: Contains the domain objects
2) PrePostBuildEvents: Console application
3) TestSuite: Contains tests for the domain objects

Project Implementation:

The class library project “MyClassLibrary” contains a single domain object called “Calculator”. Here is the implementation of the Calculator object:

public class Calculator
    {
        public double Add(double a, double b)
        {
            return (a + b);
        }
    }

The TestSuite project contains the following test:

[Test]
        public void should_be_able_to_add_two_numbers()
        {
            Calculator c = new Calculator();
            Assert.AreEqual(10, c.Add(5, 5));
        }

So, everything is pretty simple!

Now, we want that when we build our TestSuite then it should run the unit tests automatically. In the next section we will see how to attach the Post-Build event to our TestSuite project so that it will automatically run the unit tests when the project build is successful.

Attaching the Post-Build Event:

To attach a Post-Build event on the TestSuite project simply right click the project and select properties. Select the Build Events tab from the right and type the following in the Post-Build event.

call $(ProjectDir)batchfile.bat

 

The event will be fired on successful build which will call a batchfile.bat contained inside the project directory of the application. If you don’t want to run the unit test on every successful build of the TestSuite project then select “When the build updates the project type” from the drop down list. This will fire the unit tests only when some new activity happens in the project. The new activity can be adding a new file, deleting an old file etc.

Creating a Batch File:

The batch file is very simple. Take a look at the contents of the batch file:

start c:\MbUnit\mbunit.cons.exe C:\Projects\PreBuildPostBuildEvents\TestSuite\bin\Debug\

TestSuite.dll /rt:html /rf:C:\Reports\ /rnf:CalculatorTestReport

EXIT

It simply executes the MbUnit console driver and passes the parameters to run the unit tests and create a HTML report.

What About Pre-Build:

The Pre-Build is fired before starting the build process. Scott Hanselman has a very interesting post about the Pre-Build events. Take a look at the following post:

Managing Multiple Configuration Files Environment Using Pre-Build Events

Conclusion:

In this article we learned how to use the Post-Build event to run unit tests. I hope you liked the article, happy coding!