How Can I Add a Unit Test Project to an Existing MVC3 Application (from empty template)

22,415

Solution 1

To add an empty Test project just go to File>Add>New Project choose Test subcategory > Test Project.

Solution 2

The naming of the test-project is significant! If your MVC-project is named "MyProject" your test-project has to be named "MyProject.Tests" in order to connect both of them. Tools like MvcScaffolding will not work otherwise!

Solution 3

File > Add > New Project > Test > Unit Test Project, which has name: "{ProjectName}.Tests". Next step's to add References, best way's to open your {ProjectName} -> References and r-click on System.Web. Mvc, look:

enter image description here

Then will open a new window, copy path to file:

enter image description here

And add your reference :-) to unit test project:

enter image description here

Then select Browse and go to Dictionary (remember to delete filename, f.e: g:\disk\yourdll.dll = g:\disk\):

enter image description here

Next step's similar but add your {ProjectName}.dll by this way:

{ProjectName}.Tests -> References -> Add and Add {ProjectName}.dll:

enter image description here

Follow with presented steps and add references from your {ProjectName} to {ProjectName}.Tests.

Important dll:

  • System.Web.Mvc
  • System.Web.WebPages
  • System.Web.WebPages.Deployment
  • System.Web.WebPages.Razor
  • System.Web.Razor

Solution 4

For your Solution do "File->New->Project". Select "Test->Unit Test Project". Then for a name enter "YourSolution.Tests" and press OK. Next add a reference to YourSolution: on "YourSolution.Tests->References" right click and browse to Solutions, make sure that there is a check box selected for YourSolution and press Ok.

Share:
22,415
Libor Zapletal
Author by

Libor Zapletal

Updated on July 09, 2022

Comments

  • Libor Zapletal
    Libor Zapletal almost 2 years

    I created an MVC3 application from the Empty template so I couldn´t add a Visual Studio Unit test project to the solution.

    I made some changes, added some controllers and now I want to try TDD so I need to add a framework for testing.

    But I can´t see how I can do that.

    I want some way where I can create my test project right in the Solution Explorer, for example "website.Test" with some basic folders and files.

    I saw here some questions about how to add Unit tests, but those were with xUnit.net or NUnit and I want the original VS test framework.

    I am using Visual Studio 2010 Professional.

    I am also interested to know what others think about the 3rd-party unit test frameworks, are they better?

    What about for beginners?