We pay for user submitted tutorials and articles that we publish. Anyone can send in a contribution
Learn MoreLike 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".
Tags :
AppDomainClass1NUnitTest Driven .NetUnit TestsVisual Studio Copyright © 2012 Dev102.com
Breeze : Designed by Amit Raz and Nitzan Kupererd
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
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
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…
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.
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 !
Shahar Y
Said on June 25, 2008 :
Hi aniket,
Did you add nunit.core.interfaces and nunit.core assemblies?
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 ?
Shahar Y
Said on June 26, 2008 :
Don’t know… Did you add “using NUnit.Gui” ?
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
silversurfer
Said on July 14, 2008 :
I could not find Class1 but replaced it with NUnit.Gui.AppEntry.Main
JuanFran Adame
Said on November 27, 2008 :
For the third method look at:
http://nunit.com/blogs/?p=28
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.
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
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.
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
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
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.
David
Said on November 17, 2009 :
#5: Run as VS startup
http://blog.rwendi.com/CategoryView,category,NUnit.aspx
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” });
}
}
}
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
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
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.
SamuelSorge
Said on December 20, 2011 :
Hey there,
is it possible to run multiple assemblies from code in gui mode?
Thanks