Integrating/Using NUnit Test Framework with Visual Studio 2010 for ASP.NET MVC Application

ASP.NET MVC 2

The Model-View-Controller (MVC) pattern is an architectural design principle that separates the components of a Web application. This separation gives you more control over the individual parts of the application, which lets you more easily develop, modify, and test them.

ASP.NET MVC is part of the ASP.NET framework. Developing an ASP.NET MVC application is an alternative to developing ASP.NET Web Forms pages; it does not replace the Web Forms model.

If you have installed Visual Studio 2010, ASP.NET MVC 2 is already installed on your computer. You can get the most up-to-date version of the ASP.NET MVC 2 framework from the ASP.NET MVC download page.

Unit Test

If you create new ASP MVC 2 Web Application project, this is the view you get in VS 2010:

30900-vs2010-new-project.jpg

You just need to pick ASP .NET MVC 2 under a list of available templates, provide a name for your project (or stick with default, but this is definitely not recommended for any production code – you wouldn’t like to have your projects named ASP MVC app2, app3, etc….) and click OK. This is exactly the same as for any other kind of project templates available in Visual Studio. But for ASP MVC 2 there is a additional step involved:

30903-vs2010-unit-test-project.jpg

You can choose for Visual Studio to create a unit test project automatically for you. After it’s created it will contain unit tests for the controllers used in MVC project (17 unit tests all together). Out of the box Visual Studio supports adding unit test project for Visual Studio Unit Test framework only. However for some reasons you maybe prefer to use another test framework for excample NUnit, MbUint, or XUnit.

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.

Creating 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:

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

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 10.0|Common7|IDE|ProjectTemplates|C#|Test

Registering NUnit Test Framework


1.In Windows, open Registry Editor.

Caution
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_MACHINESOFTWAREWow6432NodeMicrosoftVisualStudio10.0MVC2TestProjectTemplates

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

6.Close Registry Editor.

Install NUnit Test Project Template within Visual Studio 2010


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

%Program Files%Microsoft Visual Studio 9.0Common7IDE

Note: on Windows 7 you must run Command window as Administrator,otherwise the operation will failed.

2. Enter the following command:

devenv /setup
This operation adds your test template to Visual Studio. This operation can take several minutes.
3. 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.

  • Click Test.

If the template was added correctly, you see it listed under Visual Studio installed templates.

30902-vs2010-test-project-type.jpg

Testing the 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.
30901-vs2010-Nunit-framework.jpg
Using Batch Script
To avoid manually modify windows registry and run command window, it is better to write a batch script to do this jobs. Here is an example.
NUnit.reg
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoftVisualStudio10.0MVC2TestProjectTemplatesNUnitC#]
"Template"="MVCAppNUnitTests.zip"
"Path"="CSharpTest"
"TestFrameworkName"="NUnitFramework"
"AdditionalInfo"=" http://www.nunit.org/index.php?p=home"
installNUnit.cmd
setlocal
Set CopyFrom=%~dp0
if NOT DEFINED DevEnvDir (
if Exist "%ProgramFiles(x86)%Microsoft Visual Studio 9.0Common7IDEVWDExpress" (
Set DevEnvDir=%ProgramFiles(x86)%Microsoft Visual Studio 9.0Common7IDEVWDExpress
echo Registering Templates
"regedit.exe" /s "%CopyFrom%Visual BasicNUnit64bitExpress.reg"
"regedit.exe" /s "%CopyFrom%CSharpNUnit64bitExpress.reg"
goto express
) else if Exist "%ProgramFiles%Microsoft Visual Studio 9.0Common7IDEVWDExpress" (
Set DevEnvDir=%ProgramFiles%Microsoft Visual Studio 9.0Common7IDEVWDExpress
echo Registering Templates
"regedit.exe" /s "%CopyFrom%Visual BasicNUnitExpress.reg"
"regedit.exe" /s "%CopyFrom%CSharpNUnitExpress.reg"
goto express
) else if Exist "%ProgramFiles(x86)%Microsoft Visual Studio 9.0Common7IDE" (
Set DevEnvDir=%ProgramFiles(x86)%Microsoft Visual Studio 9.0Common7IDE
echo Registering Templates
"regedit.exe" /s "%CopyFrom%Visual BasicNUnit64bit.reg"
"regedit.exe" /s "%CopyFrom%CSharpNUnit64bit.reg"
goto vsts
) else if Exist "%ProgramFiles%Microsoft Visual Studio 9.0Common7IDE" (
Set DevEnvDir=%ProgramFiles%Microsoft Visual Studio 9.0Common7IDE
echo Registering Templates
"regedit.exe" /s "%CopyFrom%Visual BasicNUnit.reg"
"regedit.exe" /s "%CopyFrom%CSharpNUnit.reg"
goto vsts
)
else (
echo Error: Cannot locate Orcas install location.
goto end
)
)
if NOT Exist "%DevEnvDir%" (
echo Error: Cannot find Orcas install at: %DevEnvDir%
goto end
)
:vsts
echo Copying Templates
if NOT Exist "%DevEnvDir%ProjectTemplatesVisualBasicTest" (
mkdir "%DevEnvDir%ProjectTemplatesVisualBasicTest"
)
if NOT Exist "%DevEnvDir%ProjectTemplatesCSharpTest" (
mkdir "%DevEnvDir%ProjectTemplatesCSharpTest"
)
copy "%CopyFrom%Visual BasicMvcApplication.NUnit.Tests.zip" "%DevEnvDir%ProjectTemplatesVisualBasicTest"
copy "%CopyFrom%CSharpMvcApplication.NUnit.Tests.zip" "%DevEnvDir%ProjectTemplatesCSharpTest"
echo Running devenv.exe
"%DevEnvDir%devenv.exe" /setup
goto end
:express
echo Copying Templates
if NOT Exist "%DevEnvDir%ProjectTemplatesVisualBasicTest" (
mkdir "%DevEnvDir%ProjectTemplatesVisualBasicTest"
)
copy "%CopyFrom%Visual BasicMvcApplication.NUnit.Tests.zip" "%DevEnvDir%ProjectTemplatesVisualBasicTest"

if NOT Exist "%DevEnvDir%ProjectTemplatesCSharpTest" (
mkdir "%DevEnvDir%ProjectTemplatesCSharpTest"
)
copy "%CopyFrom%CSharpMvcApplication.NUnit.Tests.zip" "%DevEnvDir%ProjectTemplatesCSharpTest"

echo Running devenv.exe
"%DevEnvDir%" /setup
goto end
:end
References:
[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/13651903/viewspace-1039070/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/13651903/viewspace-1039070/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值