Sunday, September 5, 2010

ASP MVC 2 – NUnit based unit test project template For Visual Studio 2008

Piotr Kwapin wrote an article about creating a NUnit based unit test project template for ASP MVC2 for Visual Studio 2010. I am still using Visual Studio 2008. This was an interesting learning experience. I never previously delved into project templates.

Piotr Kwapin mentioned this would work with Visual Studio 2008 but did not elaborate. I was able to get it working after some rather scary moments.

Download the Template

The first step is to download the template updated for Visual Studio 2008. The template is a zip file containing the following.

  • Controllers
    • AccountControllerTest.cs
    • HomeControllerTest.cs
  • Properties
    • AssemblyInfo.cs
  • App.config
  • MvcApplicationTest.csproj
  • MvcWebApplicationNUnitBasedTestProjectTemplate.cs.vstemplate

Put the template file in the template directory. On a PC running a 64-bit operating system, this directory is located here. Do not unzip or extract files from the zip.

c:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\CSharp\Test\

Download the Registration File

The next step is downloading the updated registration file. Extract the registration file and execute it. Windows will issues several warning and confirmations about running the file.

Running Devenv Setup

Piotr Kwapin did not document the last step as completely. You need to open up a command line as administrator that has the path setup to run devenv.exe. On Windows 7, select Start, All Programs, Visual Studio 2008, and then Visual Studio Tools.

 

Right click on Visual Studio 2008 Command Prompt and select Run as administrator. Enter the following command.

devenv /setup

 

The first time I did that, Visual Studio 2008 forgot about all its templates. I suggest running the command a couple of times. I ran the command around a dozen times as I made minor changes to the files to get them to work with Visual Studio 2008. I documented the changes I made to the files below.

Changes to the VSTemplate File

I changed the version number for Microsoft Visual Studio from 10.0.0.0 to 9.0.0.0. Below is the updated line.

<Assembly>Microsoft.VisualStudio.Web.Mvc.2.0, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</Assembly>

Changes to the CSProj file

I changed the tools version from 4.0 to 3.5. Below is the updated line.

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

Changes to the Registration File

I made two changes to the registration file. The first is a change to the name of the template. I compressed the template files into NUnitTestVS9.zip. The second change is the change of the HKEY to Visual Studio 2008. The two lines below show the changes.

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\9.0\MVC2\TestProjectTemplates\NUnit\C#]

"Template"="NUnitTestVS9.zip"

A New ASP.NET MVC 2 Web Application

Creating a new ASP.Net MVC 2 Web Application in Visual Studio 2008 creates both the project and allows creating a NUnit test project. Below is an example in Solution Explorer.

 

No comments: