Delen via


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.

Create unit test dialog

Note

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

  1. In Windows, open Registry Editor.

    Warning

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

  2. Locate the 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). 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".

  6. 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

  1. Create a new class library project.

    We recommend that you give the project a name that describes the test project, such as "MVCAppNUnitTests".

  2. In the new project, add references for the following DLLs:

    • System.Web.Abstractions

    • System.Web.Mvc

    • System.Web.Routing

    These DLLs are installed in the global assembly cache (GAC).

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

  4. Make sure that 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.

    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 Bin folder.

  7. In the File menu, click Export Template.

  8. Select Project template, and then click Next.

  9. 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.

  10. 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.

  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. This operation can take several minutes.

  14. To determine whether your unit-test template was added correctly, do the following:

    1. Open Visual Studio.

    2. In the File menu, click New Project.

    3. Under Project types, expand the code language node.

    4. Click Test.

    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

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

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

  3. If you entered a URL for the AdditionalInfo registry value, click Additional Info and confirm that the correct Web page is displayed.

  4. Click OK.

    A new MVC application and test project is generated.

  5. In the test project, examine the references, classes, and unit tests to make sure that they are correct.

See Also

Other Resources

ASP.NET MVC 2

ASP.NET MVC Overview