Like it or not, whoever wants to have a stable code, need to write unit tests. There are 2 main unit testing frameworks for .Net Environment: MBUnit and NUnit.  During this post, I am going to focus on NUnit and its integration with Visual Studio in particular.

nunitVS

Once we wrote our unit tests and compiled them, an assembly file is created which can be executed using the NUnit framework. We can run those tests directly from NUnit GUI or NUnit Console, but running our tests from Visual Studio is much more convenient and faster (assuming that we are in the middle of the development process with VS).

  1. The first and most obvious way is to start NUnit as an external program. This goal ExternalProgramcan be achieved by right clicking on the unit tests project -> choosing the properties option -> selecting the Debug tab -> choosing "start external program" radio button instead of "start project" (which is the default option) and enter NUnit assembly location. We are not done yet, the "command line arguments" section shall be filled too, otherwise, NUnit will not necessarily run our unit test. If NUnit has no command line arguments, it is opening the last assembly which was loaded into it. So, the command line argument shall be the location of our unit test assembly.
  2. The second way to achieve our goal is using plugins. ReSharper and Test Driven .Net are 2 great qs_RunTestsplugins (ReSharper has much more functionality and is not solely focused on the unit tests, while Test Driven .Net does), I will show my case using Test Driven .Net.  It is very simple, just right click on any test, unit test file or the project file and we get "Run Test" option in the context menu. What do we have to do now? just clicking on that menu item and our unit tests are running. It is important to mention that the plugin has more options, but this is not the subject of that post.
  3. The third way involves some code writing. First, the assembly type shall be exe and not dll and there shall be a main method in the code with the [STAThread] attribute. Then, add the "nunit.core.interfaces" assembly as a reference to the project file. In the main method call Class1.Main() with the name of our executing assembly as a parameter. Don’t you think someone in the NUnit team forgot to rename Class1 name ? I do…
   1: class Program

   2:  {

   3:      [STAThread]

   4:      static void Main(string[] args)

   5:      {

   6:          Class1.Main(new string[] { Assembly.GetExecutingAssembly().Location });

   7:      }

   8:  }

OK, so we covered some ways to run NUnit from Visual Studio. There is one more issue that shall be taken into consideration and that is, the AppDomain (Represents an application domain, which is an isolated environment where applications execute). What will the running AppDomain be in any of the possibilities mentioned above? NUnit or our unit test executable?

When we run NUnit as an external program or using a plugin, NUnit exe will be loaded as the default application domain. When NUnit is called from code (way #3), the AppDomain is our unit tests executable. Why is that so important you wonder? Well, that depends on our code, but i can warn you about one issue i ran into.

If an app.config file is used (file that is intended to store static values or settings for your application) and the running AppDomain is NUnit, we will get default values and not what’s in the configuration file. That is surely unwanted, so be aware of that, following way #3 will keep us away from this "obstacle".

Tags :

28 Responses to “3 Ways to Run NUnit From Visual Studio”


  1. David

    Said on March 27, 2008 :

    I like option #2, but occasionally use option #4, defining NUnit as VS external tool (Tools -> External Tools… -> Add):

    Title: NUnit
    Command: (path to nunit.exe)
    Arguments: $(BinDir)/$(TargetName).dll
    Initial directory: $(ProjectDir)

    This will open the current project in NUnit. Down side is that it doesn’t do all tests in solution, just the current project, but I’ve found it handy from time to time. You can setup a toolbar button for it too :)

  2. shaharyr

    Said on March 28, 2008 :

    David,

    I was not familiar with the method you introduced. It is always good to learn some new stuff.

    Thanks

  3. GadiW

    Said on April 1, 2008 :

    Speaking of AppDomains in NUnit, the NUnit runs the test in a separate AppDomain. Which means that Assembly.GetEntryAssembly() returns null.
    There are quite a few posts on that, but I had to learn it on the hard way…

  4. Ron

    Said on June 7, 2008 :

    I’ve tried Resharper (which is a great product if it didn’t crash with memory problems every other minute) and Test Driven.Net, but my favorite is sstill TestMatrix from http://www.exactmagic.com.

  5. aniket

    Said on June 25, 2008 :

    Hi I was just trying the 3rd way but I was unable to get Class1 anywhere i’m using Nunit 2.4.3,Please guide me on that !

  6. Shahar Y

    Said on June 25, 2008 :

    Hi aniket,

    Did you add nunit.core.interfaces and nunit.core assemblies?

  7. aniket

    Said on June 26, 2008 :

    yes I added them but still not getting !

    Error 1 The name ‘Class1′ does not exist in the current context F:\New Folder (2)\DispatcherVerify\WpfApplication1\TestProject\Main.cs 13 13 TestProject
    Is there any other reference required ?

  8. Shahar Y

    Said on June 26, 2008 :

    Don’t know… Did you add “using NUnit.Gui” ?

  9. aniket

    Said on June 26, 2008 :

    Hi,
    May I get ur ur testapp ?
    If u can forward it to me please forward it to
    aniket0001@gmail.com

  10. silversurfer

    Said on July 14, 2008 :

    I could not find Class1 but replaced it with NUnit.Gui.AppEntry.Main

  11. JuanFran Adame

    Said on November 27, 2008 :

    For the third method look at:
    http://nunit.com/blogs/?p=28

  12. justsomeguy

    Said on April 16, 2009 :

    I am trying to get option #1 working. Could you mention what version of VS you are using, as I do not see any of the options you are talking about in mine. Also, any chance of getting the screen shots, so I would have some idea of the arguments are suppose to look like.
    Thanx.

  13. Shahar Y

    Said on April 16, 2009 :

    @ justsomeguy

    Hi the screenshots are missing from some reason but I don’t have them right now.
    In order to get option 1 working, open the project properties window, select the Debug tab and see what I am talking about…

    Hope it helps

  14. Brian M

    Said on June 6, 2009 :

    Option #4 from David works really well for me. That’s just what I was looking for. Thanks David!

    I use Option #1 at work, but I do plan on switching.

  15. Bo Roost

    Said on June 11, 2009 :

    Another version of #option 4 is to serve the solution file to NUnit – then it will include tests from all projects. There is no graphical display – but output is directed to VS’ console output.

    Title: NUnit
    Command: (path to nunit-console.exe)
    Arguments: $(SolutionDir)\$(SolutionFileName)
    Initial directory: $(SolutionDir)

    Use Output window = true

  16. RizThon

    Said on July 20, 2009 :

    Other option #4: link to the *.nunit configuration file of your project.
    Command: path to nunit.exe
    Arguments: $(ProjectDir)$(ProjectFileName).nunit

  17. openshac

    Said on August 10, 2009 :

    Has anyone managed to option #3 working with NUnit 2.5.1?

    I’ve replaced Class1.Main with AppEntry.Main but I’m getting the following TypeInitializationException error: The type initializer ‘NUnit.Gui.AppEntry’ threw and exception.

  18. David

    Said on November 17, 2009 :

  19. Brett

    Said on January 7, 2010 :

    Option #3 will work as such:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NUnit.Gui;
    using System.Reflection;

    namespace MyAssembly.Test
    {
    class Program
    {
    [STAThread]
    static void Main(string[] args)
    {
    NUnit.Gui.AppEntry.Main(new string[] { @”C:\…MyAssembly.dll” });
    }
    }
    }

  20. JettGeek

    Said on January 14, 2010 :

    How about running the nunit-console.exe as a Post-build event for the project? This way, you know if a test failed every time you do a build and you can set every project up the way you want it to run. It may require a little more up-front effort, but you only have to do it once. Another benefit to this way of doing it is that you see immediately if the tests failed (without having to know the specifics of the failure), because your project will show as a build failure.

    I haven’t used it yet across a full solution, but a command as simple as the one below will run the default tests you’ve written and output to a project specific .xml file. You could do more post processing to display the .xml file however you see fit.

    “C:\Program Files\NUnit 2.5.3\bin\net-2.0\nunit-console.exe” $(ProjectPath) /xml:$(ProjectName)-nunitPostBuild.xml

  21. Prasanna

    Said on October 4, 2010 :

    Hi ,

    I am trying out the first method you explained. While RUn i am getting the Help Syntax window with all the command option. I am not getting the N unit GUI.
    Please help me out
    Thanks

  22. Prabu

    Said on May 3, 2011 :

    Step 1 is awesome, till now I was using resharper, but it makes VS very slow, Thank you very much for the post.

  23. SamuelSorge

    Said on December 20, 2011 :

    Hey there,
    is it possible to run multiple assemblies from code in gui mode?
    Thanks

  24. make money online home based business

    Said on February 16, 2014 :

    Your method of explaining the whole thing in this piece
    of writing is really pleasant, every one be able to easily be aware
    of it, Thanks a lot.

  25. How to Sharpen a Pocket Knife

    Said on March 14, 2014 :

    I must thank you for the efforts you’ve put in penning this site.

    I am hoping to see the same high-grade blog posts from you in the future as well.

    In truth, your creative writing abilities has
    inspired me to get my own, personal site now ;)

3 Trackback(s)

  1. Mar 27, 2008: DotNetKicks.com
  2. Mar 28, 2008: Dew Drop - March 28, 2008 | Alvin Ashcraft's Morning Dew
  3. Apr 17, 2009: Dev102.com March 2009 Roundup | Dev102.com

Post a Comment