One of the most criticized features of Visual Studio ever since it came out has been the ability, or lack and inadequacy thereof, of the IDE to integrate third-party unit testing libraries into it.
There are many views on which is the best time to write unit tests for software, some folks feel strongly that it should be after the software is written, but a growing number of people prefer writing unit tests before the production code is written. This approach is called Test Driven Development.
Figure #1 below shows the traditional way of writing unit tests. The dotted lines represent the steps that people treat as optional.
Figure # 2 : Test driven development – a bird’s eye view. Notice the spiral nature of the process. Write test, write code, refactor, write next test. It shows the incremental nature of TDD: small tests lead to quality end result.
Test driven development is different from traditional development as shown in figure 1 above. You begin by writing a test that fails, then you move on to creating the production code, seeing the test pass, and continuing on to either refactor your code or create another failing test. Previously MSTest Framework wanted users to rely on automatically generated tests put up after code has already been written. This runs so counter to the tenets of Test Driven Development that MSTest Framework was simply ignored, and many developers came up with ways to hack third party unit-testing frameworks like NUnit, MBUnit, xUnit and so on for their .NET projects. Inevitably, MSTest Framework ended upbeing vilified by the believers of TDD.
With Visual Studio 2012 we have provided a means of using frameworks like NUnit to actually be run within its IDE, in a tightly integrated way, and in turn, the ability to code test-first. Let's talk about how to make that happen.
Setting up the NUnit Test Adapter
1. Wire Visual Studio 2012's testing framework to NUnit. To be able to do this we need to download and install the NUnit Test Adapter extension, which we will do via Visual Studio's "Extension Managers" feature:
2. On the left tab click on "Online" and on the Search field on the upper right type in "NUnit Test Adapter". You will need an internet connection for this to work.
3. When the NUnit Test Adapter item appears click the "Download" button. Once the download is complete follow the instructions to install the NUnit Test Adapter
4. At the bottom of the window you may be asked to click "Restart Now" to allow the newly installed components to take effect within Visual Studio 2012.
Once you've finished this Visual Studio 2012 is now ready to run NUnit tests within the IDE.
Creating your First NUnit test project
Lets now create our first NUnit test project
3. On the left tab click on "Online" and then on the Search field type "NUnit" this time. When the result appears click "Install".
4. All the relevant classes required for NUnit should now be included in the project -- it's now time to write some unit tests!
To check if NUnit works on Visual Studio let's try adding bits of code and one dummy test:
public class NUnitTests
public void SumOfTwoNumbers()
Assert.AreEqual(10,5 + 5);
public void AreTheValuesTheSame()
Assert.AreSame(10, 5 + 6);
To run this test, click on the Test menu, Run, then All Tests. You could also use the shortcut Ctrl-R, then A. The test result under the new "Test Explorer" tab will look like this:
There are additional details that come up for failed test - highlighting the failed test reveals the expectations of the failing test, the actual result, and even the stack trace, crucial for figuring out why the test is failing.
You're now ready to use NUnit for Test Driven Development within Visual Studio 2012 IDE.
The Art of Unit Testing: With Examples in .Net