Saturday, 15 November 2008

Making your own VS.NET nunit Test Project Template

Returning to .NET has been exciting after a long time away playing in Java and Ruby land. I can't say I'll miss XML config files and all that junk.

First thing I did after creating my first ASP.NET MVC project was to add a template to easily create a project to test it. The new template will create a class library project with MVC, nunit and my mocking framework of choice Rhino included and ready to go.

When you use VS.NET 2008 to create an MVC web application, it asks if you want to add a testing project to go alongside. Unfortunately, the only choice of test project template you get is the Microsoft one and I like nunit much much more.

This is how we do it...

Set the project up the way you want it to appear new
  1. Create a new C# class library project. Give it a general name which will apply to your test project template.
  2. Add a reference to the System.Web.MVC dll.
  3. Add a reference to the nunit.framework dll.
  4. Add a reference to the Rhino.Mocks dll.
  5. Set the Copy Local property for all of the above dll references to True.
  6. Create a Controller folder under the project root.
  7. Add a class HomeControllerTest to the Controller folder.
  8. Add unit tests to cover the About and Index actions.
  9. Build your project to get the binaries.

Export the project template

  1. Go to File --> Export Template and use the wizard to export the template. Call it NUnitTest. You will be told where the zip file is saved to.
  2. Be sure to chose the option to include the icon.
Importing the new template
  1. Move the NUnitTest.zip to %Program Files%\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\Csharp\Test\1033. This might be different for you.
  2. Quit Visual Studio.
  3. Open a command prompt and navigate to %Program Files%\Microsoft Visual Studio 9.0\Common7\IDE.
  4. Execute devenv /setup and go make a cup of coffee.
  5. If you now open VS.NET 2008 and create a new project, you should be able to see your template show up under the C#-->Test directory.
  6. Add the registry key below by copying the text in to a file and calling it nunit.reg. Run this regfile to add the key. You can add it manually also using regedit.


Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\MVC\TestProjectTemplates\NUnit\C#]
"Template"="NUnitTest.zip"
"Path"="CSharp\\Test"
"TestFrameworkName"="nunit Test"
"AdditionalInfo"="http://msdn2.microsoft.com/en-us/library/ms243147(VS.80).aspx"
"Package"=""

1 comment:

JazzyJ said...

Learned something new, thanks