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.
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: 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".
We pay for user submitted tutorials and articles that we publish. Anyone can send in a contribution
Learn More
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
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
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…
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.
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 !
Shahar Y Said on Jun 25, 2008 :
Hi aniket,
Did you add nunit.core.interfaces and nunit.core assemblies?
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 ?
Shahar Y Said on Jun 26, 2008 :
Don’t know… Did you add “using NUnit.Gui” ?
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
silversurfer Said on Jul 14, 2008 :
I could not find Class1 but replaced it with NUnit.Gui.AppEntry.Main
JuanFran Adame Said on Nov 27, 2008 :
For the third method look at:
http://nunit.com/blogs/?p=28
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.
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
Brian M Said on Jun 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.
Bo Roost Said on Jun 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
RizThon Said on Jul 20, 2009 :
Other option #4: link to the *.nunit configuration file of your project.
Command: path to nunit.exe
Arguments: $(ProjectDir)$(ProjectFileName).nunit
openshac Said on Aug 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.