Directory Freebies VS CheatSheet Forum

RSS

Email

Translate

Home About Archive Privacy Contact Advertise Guest Post
Posted by Shahar Y on Mar 22nd, 2008 | Filed under C#, Visual Studio |

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: , , , , ,

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


  1. David Said on Mar 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 Mar 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 Apr 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 Jun 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 Jun 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 Jun 25, 2008 :

    Hi aniket,

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

  7. aniket Said on Jun 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 Jun 26, 2008 :

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

  9. aniket Said on Jun 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 Jul 14, 2008 :

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

  11. JuanFran Adame Said on Nov 27, 2008 :

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

  12. justsomeguy Said on Apr 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 Apr 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

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

Advertise on Dev102 Bitrix Site Manager

Read reviews and compare prices on : Laptops , Software Like Windows, Office Suites & Many More.

Advertise on Dev102
Write Article for Dev102

Write for us!

We pay for user submitted tutorials and articles that we publish. Anyone can send in a contribution

Learn More