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.
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.
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.
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
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With