Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize code and tests with asp.net mvc and unit testing

So, I'm biting the bullet and trying to get started with asp.net MVC, unit testing and TDD.

I have a vague understanding of the concepts involved, which is to say somewhat beyond the "Hello World" level, but still pretty green. I'm ready for the rubber to meet the road, but I've found myself staring at the "New Project" dialog in VS for the last half hour... how, exactly, do you organize your unit tests?

I see that with the standard VS Unit Test project type, it creates a separate project for the unit tests. Is that how I should proceed when using NUnit? Or should the tests be placed in the same project as the code being tested?

None of the "getting started with unit testing..." type tutorials I've found seem to address this.

like image 761
seanicus Avatar asked Mar 29 '11 18:03

seanicus


People also ask

Which tool is used for unit testing in ASP .NET MVC?

Visual Studio is used to create test code. It is also used to run the test code for an ASP.Net application. In this way, it becomes simple to check for any errors in an ASP.Net application. In Visual Studio, the testing module comes with an out of box functionality.


3 Answers

Tests should be maintained in separate projects because you don't want to deploy testing code to a production environment. For a single-project solution, one test project is probably sufficient.

Internally, a test project can be organized in any way you find convenient, as long as it's consistent. An ASP.NET MVC test project might have a ControllerTests folder with one test .cs file per controller, mirroring the MVC project structure to some degree. This makes the tests easy to find and relate to the code they're testing.

like image 99
Dave Swersky Avatar answered Oct 17 '22 01:10

Dave Swersky


how, exactly, do you organize your unit tests?

I organize my unit tests to reflect my project structure. So fir example if in my ASP.NET MVC project I have

  • Controllers
  • Models
  • Mappers
  • Validators ...

I have those same folders in my unit test project. Then for each file in the MVC project I have a corresponding unit test. Here's an example of a sample project structure I wrote.

like image 24
Darin Dimitrov Avatar answered Oct 16 '22 23:10

Darin Dimitrov


My format has usually been like so:

MyMvcApp.Web (The Actual Web Application)
  |- Controllers
  |- ViewModels
  |- Views
  |- Framework (For specific override points in the MVC Framework)
MyMvcApp (The class library that contains my domain specific logic)
  |- SomeFacet (Folder to contain entities, objects, etc)
   |- Repositories
MyMvcApp.UnitTests (Test project)
  |- SomeFacet (Contains tests for specified folder in class library)
MyMvcApp.IntegrationTests (Test project)
  |- SomeFacet (Contains tests for specified folder in class library)
like image 43
Tejs Avatar answered Oct 17 '22 00:10

Tejs