Test Driven Development using NUnit natively in Visual Studio 2012


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 up
being 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

  1. Go to File -> New -> Project and under Visual C# choose "Class Library" and rename to "NUnitTests". You may also want to rename your "Class1.cs" into something more sensible e.g.NUnitTests
  2. In the Solution Explorer right-click on references then click "Manage NuGet Packages".



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:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using NUnit.Framework;


namespace TailSpinToys.UnitTesting



    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.


Happy Coding.





The Art of Unit Testing: With Examples in .Net


Comments (14)

  1. Hemanand Rajamani says:

    Hi I followed  your steps but i couldn't see any test results on the test explorer. Kindly help

  2. Randhir says:

    Hi – When typed in the Nunit Test Adapter in the search field I am anot bale to see anything related to Nunit test adapter. I am using Visual studio express 2012. Please help

  3. Uzair says:

    I am experiencing the same issue as Hemanand. When I build my solution, it notifies that build is successful. But My tests are not visible in solution explorer. And nothing happens when i run my tests from solution explorer.

    P.s. I am using VS 2012 Update 3.

  4. Roger Torhov says:

    Are your test Methods and class Public? I have read that the test class and Methods has to be Public to make them show up.

  5. Muneeb says:

    What is the procedure to write nunit test cases on a website??

  6. JS says:

    Restart your VS, should do the trick

  7. Chris says:

    My problem is, that the visual studio is not showing my nunit tests in the test explorer. I have follow this description but it doesnt work. Please Help

  8. Ahmed Abokhalifa says:

    After building, go to the Test tab and select "Run All Tests"

  9. Michael Jones says:

    I have this working, but it is incredibly slow during the test enumeration phase.

  10. Ankit Seth says:

    If the tests are not showing in your test explorer even though you have marked the tests with [Test] or [TestCase] attribute and the test class with [TestFixture] attribute then you need to add the NUnit test adapter to your Visual Studio IDE.

    You can go to Tools–>Extensions and Updates—->Online and download and install the NUnit Test Adapter.

    Afterwards restart your Visual Studio

    Then enable the TestExplorer through Test—>Windows—>TestExplorer

    Hope it helps!

  11. using NUnit in Visual Studio is a step backwards says:

    First of all NUnit does not seem to install correctly in Visual Studio 2013. After following the above procedure the IDE complains that NUnit name space has not been found.

    But what is more important the Visual Studio has its own method of providing testing environment, much better than NUnit.

    Why then struggle to use NUnit ?

  12. NGREYES says:

    NUnit Provides the means to test in parallel whereas Unit Test in VS does not.  That's the advantage.  When I say in parallel I mean multiple machines running the same tests basically at once and not sequentially.

  13. John Trimming says:

    Has anyone found a solution to NUNit not installing correctly on VS 2013?

Skip to main content