Adding a Custom MVC Test Framework in Visual Studio

Introduction

Visual Studio 2008 provides a test framework named Visual Studio Unit Test in all editions except Standard and Express.. However, many test developers are already familiar with third-party test frameworks such as NUnit, MbUint, XUnit. Developers might also be using a third-party mock object library, such as Rhino mocks, Type mocks, or NMock.

This topic explains how to add a third-party test framework and mock object library to Visual Studio for testing ASP.NET MVC applications.

Adding a new MVC test framework is a two-stage process. First, you create a test project template. You then add the template to the Windows registry. 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.

Creating a Template for an MVC Test Project

First, you will need to create a new project template that references any third-party framework and mock object library that you intend to include. For more information about creating a project template, see Creating Project and Item Templates.

Before starting the following procedure, you should download and install the test framework and mock object libraries you want to add.

To create a template for an MVC test project

  1. Create a new Class Library project using the code language of your choice.

    It is a good idea to give the project a name that describes your test project template, such as example "MVCAppNUnitTests".

  2. Open the new project and add references for the following DLLs:

    • System.Web.Abstractions

    • System.Web.Mvc

    • System.Web.Routing.

    The DLLs are in the following folder:

    %Program Files%\Microsoft ASP.NET MVC Beta 1\Assemblies

  3. Add references for the DLLs of the test framework and mock object libraries.

  4. Make sure the Copy Local property for all references that you have added is set to true.

  5. Add the folders and classes that your template requires.

    You can add whatever folders and classes you want. At a minimum, we recommend that you add the following folders and classes for testing the default MVC application:

    1. Add a Controller folder and a Routes folder under the project root.

    2. In the Controller folder, add a class named HomeControllerTests.

    3. Add unit test methods to the HomeControllerTests class for testing the Index and About action methods.

    4. In the Routes folder, add a class named RouteTests.

    5. Add unit test methods to the RouteTests class for testing the default routes.

  6. Build the project so that the required binary files are copied to the Binfolder.

  7. In the File menu, click Export Template.

  8. Select Project template, and then click Next.

  9. Clear the check box marked Automatically import the template into Visual Studio, 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.

  10. Move the exported template to the following folder:

    %Program Files%\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\Language\Test\Locale

    Substitute the appropriate values for Language and Locale.

  11. Close all instances of Visual Studio.

  12. Open the Windows Command window and change to the following folder:

    %Program Files%\Microsoft Visual Studio 9.0\Common7\IDE

  13. Enter the following command:

    devenv /setup

    This operation adds your test template to Visual Studio. Note that this operation can take several minutes.

    To see if your test template was added properly, open Visual Studio. In the File menu, click New Project. Under Project types, expand the code language node and click Test. You should see your template list under Visual Studio installed templates.

This procedure creates a test project template and adds it to VS. However, the template does not appear as a test framework option when you create a new MVC application. To do that, you must add information about your template to the Windows registry.

Adding the Template Information to Windows Registry

ASP.NET MVC stores information about templates in the Windows registry. It uses this information when it displays the Create Test Project dialog box. For your template to appear as an option in the Test framework list, you need to add the appropriate keys and values to the registry.

Warning

You should back up your Windows registry before changing any registry settings.

To add the template information to the Windows registry

  1. In Windows, open the Registry Editor (Regedit.exe).

    Note

    We recommend that you back the registry by exporting it to a file before making changes to it.

  2. Locate following key:

    HKEY_LOCAL_MACHINE\SOFTWARE\VisualStudio\9.0\MVC\TestProjectTemplates

  3. Under TestProjectTemplates, add a key for the test framework, such as "NUnit".

  4. Under the test framework, add a key for the code language, such as "C#" or "VB".

  5. Add the following string values to the test framework key:

    • AdditionalInfo – (Optional) You can enter a URL that links to a Web page about the test framework. When a user clicks the Additional Info link in the Create Test Project dialog box, the Web page that you specify is displayed.

    • Package – (Optional) You can enter the path to a Visual Studio package. You might need this if your test template adds UI elements, provides localized resources, or provides other advanced functionality.

    • Path – The Visual Studio test framework expects your test templates to be registered with Visual Studio and located in the standard location for Visual Studio project templates:

      %Program Files%\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates

      You can enter a partial path to your test template that assumes the default location as the base locationCSharp\Test.

    • Template – Enter the file name of your test template, such as MVCApplicationNUnitTests.zip.

    • TestFrameworkName – Enter the name of the test framework as you want it to appear in the Test framework list of the Create Test Project dialog box.

    To localize the URL in AdditionalInfo or the framework name in TestFrameworkName, add a prefix with a pound sign ("#") followed by the language code and a colon (":"). For example, you might enter the name of the test framework as "#1209:NUnit". This tells Visual Studio to use resource number 1209 from Package for the localized string; otherwise, use "NUnit".

  6. Close Registry Editor.

Testing Your Test Project Template

After you register your new test template with Visual Studio and enter the template information in the Windows registry, you can test your template by creating a new MVC application.

To test your test project template

  1. In Visual Studio, create a new ASP.NET MVC application.

  2. In the Create Test Project dialog box, select your new test framework on the Test framework list.

  3. If you entered a URL for the AdditionalInfo registry value, click Additional Info. After viewing the Web page, close it.

  4. Click OK. A new MVC application and test project is generated.

  5. In the test project, examine the references, classes, and unit tests for correctness.

This topic is ASP.NET 3.5 Extensions pre-release documentation and is unsupported by Microsoft. Blank topics are included as placeholders and existing content is subject to change in future releases. To provide feedback or ask questions about the release, please go to the ASP.NET 3.5 Extensions forums.