Use Visual Studio's Post-Build Events to Automate Unit Testing Running

Whether you're into test driven development (TDD) or test after development (TAD), or you're into having your code get br0xored by not testing (doh!)... unit testing is all over the development world. You're a silly-goose if you haven't heard about it. In any case, using Visual Studio's post-build event process is a nice way of making sure your tests are always executed after a build. Though this isn't news to most developer 'dudes,' it surely made me all hot and steamy when I did it!

To do it you'll need the following:

  • Visual Studio (no duh)
  • Some sort of command line unit test app... like nunit console
  • A new pair of pants after you see how rad this is.

So, you go here:
Visual Studio -> Project Properties -> Build Events

And add this line, pointing nunit-console to the DLL of your project:

"C:\Program Files\nunit\nunit-console.exe" "C:\SVN\Proj\trunk\ProjTestProj\bin\Release\ProjTestProj.dll"

And that's it! Get ready for the hotness!

But wait! There's more!

As an added bonus... I add this line before the line above...

copy /Y "$(ProjectDir)Web.config" "C:\SVN\Proj\trunk\ProjTestProj\App.config"

This enables "Unit testing" (or more like expectation testing) of the Web.config file by copying it over to the Unit Test project as App.config. When this happens, you would be able to talk to it in your Test project via the ConfigurationManager. For example:

Assert.AreEqual("666", ConfigurationManager.AppSettings["SomeImportantNumber"]);

kick it on

Heavy Metal Rock and Roll.

thats a nice way

i can actually use it... i think its pretty good
thank you...


Submitted by edi (not verified) on Sun, 06/27/2010 - 11:35am.
Another solution

Hi there,

I've also worked on that today and here's a link to the solution I've come up with. As an added bonus you'll get the ... propagated with unit test errors that you can click and it'll bring you directly to the place in code that caused the problem.


Submitted by Padcom (not verified) on Tue, 12/23/2008 - 2:02am.
How i can type several files?

for example i need following:
Packer.exe -o "C:\4\*.js" -m packer "$(ProjectDir)Scripts\*.js"
* - one of file in my directory

Submitted by (not verified) on Tue, 10/07/2008 - 9:38am.
NM fixed the issue

had to change my copy to:

copy /Y "$(ProjectDir)app.config" "$(SolutionDir)FNMGUnitTests\$(OutDir)FNMGUnitTests.dll.config"

and load the dll and not the project in NUnit.

Create article btw.

Submitted by Rich Hudson (not verified) on Sat, 07/05/2008 - 5:20am.
Having an issue with similar setup

I have an app.config in a project which I use, and I copy it to the nunit test project in the same solution as you do in the above example. Problem is for the life of me I cannot get anything other than null (StringAssert fails) I have tried everything. If I call the test code directly I get the value, soon as I let Nunit run, I get null. Any ideas?

Rich Hudson

Submitted by Rich (not verified) on Sat, 07/05/2008 - 4:29am.
brian's picture
For thee

Submitted by brian on Wed, 05/21/2008 - 1:29am.
brian's picture

Thanks for that kick in the junk. You're very right... I need to get ncover and FxCop rockin' too. I've used FxCop, but not as part of the build process. I've been looking at ncover for some time now (the non-enterprise version) and I'll be hooking that up as well. I'll be back with that soon :)

Submitted by brian on Tue, 05/20/2008 - 8:13pm.

Very nice.... I have done "copy" in post-build too. Good idea

Submitted by Anonymous on Mon, 05/19/2008 - 10:13pm.
Coverage and FxCop too

The next step is to automate coverage at the same time. Oh, and saving having to type the explicit path to the unit test assembly.

Put this in the unit test assembly post-build...

set NCOVER=<path to>\Ncover.console.exe
set NUNIT=<path to>\nunit-console.exe
set TEST=$(TargetPath)
echo on

And you are running FxCop over the assemblies under test in their post-build, aren't you?

Submitted by Anonymous on Mon, 05/19/2008 - 3:47pm.
brian's picture

That is a great idea!

Submitted by brian on Sat, 05/17/2008 - 1:32pm.
Slight modification...

We keep a copy of NUnit in a project lib folder. So we call it as follows:

$(ProjectDir)\lib\nunit-bin\nunit.exe $(TargetPath) /run

That will startup the gui runner and run all the tests.

Thanks... great post!

Submitted by Anonymous on Fri, 05/16/2008 - 10:05pm.

About Brian

profile for bluevoodoo1 on Stack Exchange, a network of free, community-driven Q&A sites

Brian Canzanella brings you nifty tips and tricks for most things .NET. read more...

Readers / Stuff