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"]);
Heavy Metal Rock and Roll.

















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.
http://padcom13.blogspot.com/2008/12/running-unit-tests-during-builds-in.html
Bye!
- reply
Submitted by Padcom (not verified) on Tue, 12/23/2008 - 2:02am.for example i need following:
Packer.exe -o "C:\4\*.js" -m packer "$(ProjectDir)Scripts\*.js"
* - one of file in my directory
- reply
Submitted by asp.net (not verified) on Tue, 10/07/2008 - 9:38am.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.
- reply
Submitted by Rich Hudson (not verified) on Sat, 07/05/2008 - 5:20am.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
- reply
Submitted by Rich (not verified) on Sat, 07/05/2008 - 4:29am.http://www.csharptocsharp.com/visual-studio-automate-testing-part-two
- reply
Submitted by brian on Wed, 05/21/2008 - 1:29am.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 :)
- reply
Submitted by brian on Tue, 05/20/2008 - 8:13pm.Very nice.... I have done "copy" in post-build too. Good idea
- reply
Submitted by Anonymous on Mon, 05/19/2008 - 10:13pm.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
%NCOVER% //reg %NUNIT% %TEST%
And you are running FxCop over the assemblies under test in their post-build, aren't you?
- reply
Submitted by Anonymous on Mon, 05/19/2008 - 3:47pm.That is a great idea!
- reply
Submitted by brian on Sat, 05/17/2008 - 1:32pm.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!
- reply
Submitted by Anonymous on Fri, 05/16/2008 - 10:05pm.