Google analytics script

Latest jQuery CDN with code tiggling.

Wednesday, 11 January 2012

Running or debugging NUnit tests from Visual Studio without any extensions

If you write unit tests and use NUnit test framework this may be helpful. I decided to write this simple step by step project configuration because I tend to set it up on every new project but keep forgetting all its details of how to do this. Setting it up is simple and a one-time only process for each test project.

Getting NUnit

You can always get NUnit test framework directly from the web on and install it on your machine but it may be better to use a more solution-specific approach by using NuGet NUnit package that installs it only in a particular solution. Getting it using NuGet can either be done as outlined on the linked package page by using package manager console or using the package manager GUI by doing this:

  1. Right-click on project in Visual Studio Project Explorer; a popup context menu appears;
    Context menu
  2. Click on Manage NuGet Packages... menu option which will open package manager simple to use GUI;
  3. Wait for the dialog window to load packages from the web repo and then select NUnit; NuGet Package Manager

This will automatically add NUnit library and tools to your solution folder and add relevant assembly references in your test project. Having a solution-specific NUnit version makes it future proof if newer NUnit versions are not backwards compatible. So one less thing to think about.

Executing unit tests

NUnit is a fine library, but it offers no Visual Studio integration per-se. It has an external program compiled either as a console app or a GUI. It runs your unit test assembly and executes any unit tests within.

Developers not having additional non-free Visual Studio tools usually run NUnit GUI tool and run those tests manually. But it's much easier if you can easily just hit F5 or Ctrl-F5 in Visual Studio that would compile unit tests and run the NUnit GUI tool automatically for you while also executing all unit tests on application start. The good thing is that you can also debug your unit tests this way in case you wanted to check something that seems to be invalid in your test code.

Project configuration

In order to automatically start NUnit test runner GUI tool you have to follow these steps to configure your test project to be capable of running/debugging unit tests:

  1. Open your test project properties by right-clicking on project in Visual Studio Project Explorer ans selecting option Properties...;
  2. Select Debug tab in project properties window;
    Debug project properties
  3. Set Start action as Start external program and point it to your local solution NUnit package installation; if you used NuGet package this is where you'll find your NUnit folder: your solution folder\packages\nunit2.5.10.11092\tools\nunit.exe Of course with the right NUnit version in folder name.
  4. Set Command line arguments to point to your test project assembly file like: YouTestProjectAssemblyName.dll /run
  5. Set Working directory to point to your test project's debug folder like: YourTestProjectFolder\bin\Debug
  6. Save project configuration properties and you're done;

I usually also configure my solution so that I set Startup project as Current selection. This makes it possible that hitting F5 runs my currently selected project. If I have a unit test file open it will run (start debugging) my unit test project, but if I'm currently working on ie. my web application file, it will automatically start debugging my web application. Solution properties

Debugging test assemblies compiled for .Net 4.0

If you're trying to debug your test assembly that is compiled for :net version 4.0 or later you may have problems hitting your breakpoints. Visual Studio simply says that no debug symbols have been loaded for your assembly. The problem is that NUnit is running under .Net 2.0, but your test assembly is compiled for .Net 4.0.

To still keep the capability of automatically running your tests by hitting F5 you will have to edit nunit.exe.config that's in the same folder as nunit.exe test runner GUI. All you have to do is to add these three lines directly under <configuration> configuration element:

   1:  <startup>
   2:      <supportedRuntime version="4.0" />
   3:  </startup>

This is all you need to know to configure your project to run NUnit unit tests directly from Visual Studio by starting NUnit test runner GUI. If you have any additional suggestions regarding this, please leave your comment below.


  1. You need to also add

    to the nunit.exe.config in order for this to work

    1. You probably meant:

      <supportedRuntime version="4.0" />

      This isn't necessary when you're debugging test assemblies compiled for .net 3.5 or earlier. And if you're just executing your tests you don't need this either whatever .net version your assembly was compiled for.

      But if you do need to debug a .net 4.0 test assembly, then yes, you do need this additional NUnit configuration. I've seen sometimes people referring to requiredRuntime instead of supportedRuntime, but I've tried latter and it works. At least with the latest NUnit it does.

      I'll update my post to include this information. Thank you.

  2. Last thing helped. Thanks very much.

    Actually I got to remove line for version 2 and some "legacy" attribute from startup tag. (I already had node for version 4)

  3. I am using latest nunit 2.6.1. I can run nunit from vs2010 but coud not define break point inside test method. shows message: no breakpoint will currently be hit. no symbols have been loaded for this document.

    Any idea why?

    1. Did you compile in debug mode?

    2. Thanks for reply.

      Yes - Mode is debug & withuout compile optimisations.

    3. Settings/Test Loader/Assembly Isolation/Run tests directly in the NUnit process.
      If not, you have to set the debugger manually to the nunit-agent process ... each time !

    4. Another thing, even if the "Run tests directly in the NUnit process" option is checked, if the new option in 2.6 "Settings/Test Loader/Runtime Selection/Set default runtime..." is checked, it may run an agent process, so no automatic debug possible.
      Search for a nunit-agent.exe process during the test to be sure...

  4. ... and without compile optimisations?

  5. I recently installed VS 2012, I was already using Visual Nuinit with VS 2010, but in VS 2012, the Nunit option is not available in the View-> Other Windows options. Please help.

    1. You've had that menu option because you were likely using Visual nUnit Visual Studio extension. Unfortunately it seem it's not supported on VS2012 (as of yet).

      And my blog post is not about this particular plugin. Actually it's not about any plugin. It's about how to run nUnit tests simply ba pressing F5 in Visual Studio on a test project...

  6. The best solution i've encountered. Thanks alot!

  7. awesome solution........
    I searched in internet and all suggested for third party tools but your the simple and the best solution as it doesn't include any other program to install....

    Thank you so much.... :)

  8. this path didn't exist for me:-

    your solution folder\packages\nunit2.5.10.11092\tools\nunit.exe

    there was no tools folder

    1. Change to whatever folder your NUnit runner is located.

  9. I'm using Nunit.2.6.3 installed via NuGet and I want to run in debugging mode but as Graham Hobson says the suggested path for "start external program" in the project debug properties does not exist. I'm not able to find nunit.exe anywhere in the solution. Please could you advise.

    1. That's likely because you installed NUnit NuGet package that's just a library that provides testing features to your app. You also need to install NUnit.Runners package that adds NUnit GUI runner that can be used for this scenario.

  10. Thanks a lot.
    Perfect solution. Now I can debug and hit the breakpoint for the NUnit test cases

  11. Thank you very much indeed, works perfectly.

  12. Worked like a charm! Thanks a lot

  13. Thanks a lot works on Visual Studio 2013

  14. I had an issue with NUnit starting, but throwing an error "This assembly was not built with any known testing framework". The answer is that I had to put the Command line argument in quotes, since there were spaces in the folder path.



    Just in case anyone else has a not-enough-coffee moment like me!

    1. Actually, I really haven't had enough coffee - there are no spaces - but it still needs quoted!

  15. thanks a lot for this interesting article

  16. Which type of project are you using for the "Test" one.??

    1. The usual Class Library because all it will have are classes with test methods.

  17. Hi,
    I am getting the error Cannot start test project 'MOMUnitTests' because the project does not contain any tests

    1. The error says it all. You apparently don't have any classes nor methods in the assembly decorated with test-related attributes. And that's the result.

  18. Hello, I'm missing something about the use of NUnit test framework...
    Question A:
    If I want to use NUnit as testing framework for my c# class library I have done the following steps: I have created a Test Project from VS, I have deleted the reference to "Microsoft.VisualStudio.QualityTools.UnitTestFramework", and through the "Manage NuGet Packages" project option now I'm able to use the NUnit framework.
    So if I want run my test there is no problem via Visual Studio... but what about if I want to execute my test methods or test classes via the NUnit GUI? Have I to install on my PC NUnit by downloading it from the NUnit web site???

    Question B:
    If inside my project I need to add a class to extend NUnit (it means create a class that implements IAddin and EventListener) my test assembly must also reference 2 extra dlls as well as nunit.framework.dll, in particular
    - nunit.core.dll
    These 2 extra dlls are not inside the "packages" folder that I obtained through the "Manage NuGet Packages" project option. In this case have I to install NUnit on my PC and than reference these dlls by browsing my PC system folders??? ... and in this case, if I install NUnit can I delete the "packages" folder???

    Question C:
    Are there differences between your solution (using the "Manage NuGet Packages" project option) and the option which consists in installing the NUnit test adapter for the single project ( ???

    Thanks for your help.

  19. Thank you so much providing detailed instructions. This helped me so much!

  20. For those of you not getting your breakpoints hit, there are multiple reasons why this may happen. But one likely cause is f your application's target framework is different from NUnit's, then you won't be able to debug, because the tests are actually being run by nunit-agent.exe.

    For me, my application was using 4.5, but NUnit's was using 3.5. (You can find this from one of the NUnit GUI applications -> Help -> About)

    To fix this, change the nunit.exe.config file to include the following, inside the configuration section:

    1. You should escape HTML-encode XML fragments in order to keep them displayed in here within a comment.

  21. Thank you so much! Worked perfectly and just what I needed.