How to: Add a Custom ASP.NET MVC Test Framework in Visual Studio
Microsoft Visual Studio provides the Visual Studio Unit Test framework in all editions except Standard Edition and Express Edition. However, many developers and testers are already familiar with third-party test frameworks such as NUnit, MbUint, or XUnit. Developers might also be using a third-party mock-object library, such as Rhino mocks, Type mocks, or NMock.
In versions of Visual Studio that support unit-test projects, you can create a custom test project template that will then be available as a project option in the MVC project dialog box. The custom test project can use a unit-test framework that you specify. In addition, you can include other libraries in the custom test project, such as a mock-object framework, a personal library of unit-test code, and so on.
Adding a new MVC test framework is a two-stage process. First, you register the third-party test framework with the MVC test dialog box, so that it can be selected when you create a unit-test project. You then create a test-project template for Visual Studio. When you are finished, you will be able to create a test project that is based on the newly added framework whenever you create a new MVC application.
Registering a Third-Party Test Framework
ASP.NET MVC stores information about available unit-test frameworks in the Windows registry. ASP.NET MVC uses this information when it displays the Create Unit Test Project dialog box. For a test framework to appear as an option in the Test framework list, you must add the appropriate keys and values to the registry.
The following illustration shows the Create Unit Test Project dialog box.
Before you start the following procedure, make sure that you have installed the test framework and mock-object libraries that you want to add.
To register a third-party test framework
In Windows, open Registry Editor.
We recommend that you back up the registry by exporting it to a file before you make any changes to it.
Locate the following key:
Under TestProjectTemplates, add a key for the test framework, such as "NUnit".
Under the test framework, add a key for the code language, such as "C#" or "VB".
Add the following string values to the test framework key:
AdditionalInfo (Optional). Enter a URL that links to a Web page about the test framework. When a user clicks the Additional Info link in the Create Unit Test Project dialog box, the Web page that you specify is displayed.
Package (Optional). Enter the path of a Visual Studio 2008 package. You might need this if your test template adds UI elements, provides localized resources, or provides other advanced functionality.
Path. Enter the path where the template is located. The Visual Studio test framework requires that your test templates be registered with Visual Studio and located in the standard location for Visual Studio project templates, which is the following folder:
%Program Files%\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates
Template. Enter the file name of your test template, such as MVCApplicationNUnitTests.zip.
TestFrameworkName. Enter the name of the test framework the way that you want it to appear in the Test framework list of the Create Unit Test Project dialog box.
To localize the URL in AdditionalInfo or to localize the framework name in TestFrameworkName, add a prefix with a number sign (#) followed by the language code and a colon (:). For example, if you enter the name of the test framework as "#1209:NUnit", Visual Studio uses resource number 1209 from the test framework package for the localized string. If you do not want to localize the URL, use the string "NUnit".
Close Registry Editor.
Creating a Template for an MVC Test Project
After you have registered the unit-test framework, you can create a test-project template that includes the framework. For general information about how to create a project template, see Creating Project and Item Templates.
To create a template for an MVC test project
Create a new class library project.
We recommend that you give the project a name that describes the test project, such as "MVCAppNUnitTests".
In the new project, add references for the following DLLs:
These DLLs are installed in the global assembly cache (GAC).
Add references for the DLLs of the test framework and mock-object libraries.
Make sure that the Copy Local property for all references that you have added is set to true.
Add the folders and classes that your template requires.
At a minimum, we recommend that you add the following folders and classes for testing the default MVC application:
Add a Controller folder and a Routes folder under the project root.
In the Controller folder, add a class named HomeControllerTests.
Add unit-test methods to the HomeControllerTests class for testing the Index and About action methods.
In the Routes folder, add a class named RouteTests.
Add unit-test methods to the RouteTests class for testing the default routes.
Build the project so that the required binary files are copied to the Bin folder.
In the File menu, click Export Template.
Select Project template, and then click Next.
Clear the Automatically import the template into Visual Studio check box and then click Finish.
The project template is exported as a compressed (.zip) file. A Windows Explorer window opens and displays the contents of the folder where the exported template is.
Move the exported template to the following folder:
%Program Files%\Micrososft Visual Studio 9.0\Common7\IDE\ProjectTemplates\Language\Test\Locale
Substitute the appropriate values for Language and Locale.
Close all instances of Visual Studio.
Open the Windows Command window and change to the following folder:
%Program Files%\Microsoft Visual Studio 9.0\Common7\IDE
Enter the following command:
This operation adds your test template to Visual Studio. This operation can take several minutes.
To determine whether your unit-test template was added correctly, do the following:
Open Visual Studio.
In the File menu, click New Project.
Under Project types, expand the code language node.
If the template was added correctly, you see it listed under Visual Studio installed templates.
Testing the Project Template
After you enter the template information in the Windows registry and register your new test template with Visual Studio, you can test your template by creating a new MVC application.
To test the test-project template
In Visual Studio, create a new ASP.NET MVC application.
In the Create Unit Test Project dialog box, select your new test framework in the Test framework list.
If you entered a URL for the AdditionalInfo registry value, click Additional Info and confirm that the correct Web page is displayed.
A new MVC application and test project is generated.
In the test project, examine the references, classes, and unit tests to make sure that they are correct.