Integrating/Using NUnit with Visual Studio 2008 Standard and ASP.NET MVC
If you're one of the fortunate that has access to Visual Studio 2008 Professional editions or above, you might be asking, "Why would you want to use NUnit with Visual Studio 2008 Standard?". The answer is, the Standard edition (as well as Express editions) don't come integrated with a unit testing framework, nor do they come with unit testing project templates for ASP.NET MVC like the Professional edition and above. Unit testing has become more popular lately, especially with the advantages it adds for ASP.NET MVC.
However, if you're like me and can't afford the more pricey editions, there are some third party unit testing frameworks you can still use. NUnit is not your only choice, but is one of the more common frameworks and is the one I use, so I'll tell you how to get it working within Visual Studio 2008 Standard (or Visual Web Developer Express) along with debugging support while running your unit tests in ASP.NET MVC projects.
Download and install the latest version of NUnit at http://www.nunit.org. At the time of this article, I'm using NUnit version 2.5.3.
Now you need to install the unit test project templates for C# and/or Visual Basic. I got the unit testing ASP.NET MVC project templates from Joe Cartano's blog post, but I had to slightly edit Joe's template install script "installNUnit.cmd" to make it compatible with Windows 7 (The command shell interpreter has problems with using inner-parentheses within if/else blocks whose blocks are defined using parentheses), so you might have to make some slight changes to the installNUnit.cmd script to get it working for your configuration.
If successful, when you reopen Visual Studio, you should see the unit test project under New Project -> Visual C# (or Visual Basic) -> Test -> MvcApplication.NUnit.Tests
Before creating this unit test project, create an ASP.NET MVC project, then add the unit test project to that solution. Creating a project from this unit test template will create a project with output type of library. Go into the project properties and change Output Type to a Console Application, add a reference to the nunit-gui-runner.dll assembly, then add a class file to the project and define it as such:
Now when you run the unit test project in the debugger in the IDE, it will start the NUnit GUI, and since you are running NUnit from within your code in the IDE, you'll even be able to step through code in the debugger as you run your unit tests from within the NUnit GUI.
However, if you're like me and can't afford the more pricey editions, there are some third party unit testing frameworks you can still use. NUnit is not your only choice, but is one of the more common frameworks and is the one I use, so I'll tell you how to get it working within Visual Studio 2008 Standard (or Visual Web Developer Express) along with debugging support while running your unit tests in ASP.NET MVC projects.
Download and install the latest version of NUnit at http://www.nunit.org. At the time of this article, I'm using NUnit version 2.5.3.
Now you need to install the unit test project templates for C# and/or Visual Basic. I got the unit testing ASP.NET MVC project templates from Joe Cartano's blog post, but I had to slightly edit Joe's template install script "installNUnit.cmd" to make it compatible with Windows 7 (The command shell interpreter has problems with using inner-parentheses within if/else blocks whose blocks are defined using parentheses), so you might have to make some slight changes to the installNUnit.cmd script to get it working for your configuration.
If successful, when you reopen Visual Studio, you should see the unit test project under New Project -> Visual C# (or Visual Basic) -> Test -> MvcApplication.NUnit.Tests
Before creating this unit test project, create an ASP.NET MVC project, then add the unit test project to that solution. Creating a project from this unit test template will create a project with output type of library. Go into the project properties and change Output Type to a Console Application, add a reference to the nunit-gui-runner.dll assembly, then add a class file to the project and define it as such:
using System; using System.Reflection; namespace TestMvc.Tests { class Program { [STAThread] static void Main(string[] args) { NUnit.Gui.AppEntry.Main(new string[] { Assembly.GetExecutingAssembly().Location }); } } }
Now when you run the unit test project in the debugger in the IDE, it will start the NUnit GUI, and since you are running NUnit from within your code in the IDE, you'll even be able to step through code in the debugger as you run your unit tests from within the NUnit GUI.
Comments: (0)
Alphatrak
"Bringing you the coding smackdown since '95"
"Bringing you the coding smackdown since '95"
Now Playing
- Dragon Age: Origins - PC
- World of Warcraft - PC
Now Reading
- Wrox Beginning ASP.NET MVC 1.0
- Game Coding Complete, 3rd Ed.
- Writing Secure Code, 2nd Ed.
- Dragonlance Preludes: Kendermore