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).
- The first and most obvious way is to start NUnit as an external program. This goal
can 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. - The second way to achieve our goal is using plugins. ReSharper and Test Driven .Net are 2 great
plugins (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.
                                                - 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”.
- How to Compile from Windows Explorer / Total Commander
- How to Solve error CS0103: The name ‘InitializeComponent’ does not exist in the current context
- How to Get Visual Studio to Create New Classes Public by Default
- Free Tool - Extract Hard Coded Strings to Resource Files
- A Small and Fast Domain Modeling Tool
By David on Mar 27, 2008 | Reply
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
By shaharyr on Mar 28, 2008 | Reply
David,
I was not familiar with the method you introduced. It is always good to learn some new stuff.
Thanks
By GadiW on Apr 1, 2008 | Reply
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…
By Ron on Jun 7, 2008 | Reply
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.
By aniket on Jun 25, 2008 | Reply
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 !
By Shahar Y on Jun 25, 2008 | Reply
Hi aniket,
Did you add nunit.core.interfaces and nunit.core assemblies?
By aniket on Jun 26, 2008 | Reply
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 ?
By Shahar Y on Jun 26, 2008 | Reply
Don’t know… Did you add “using NUnit.Gui” ?
By aniket on Jun 26, 2008 | Reply
Hi,
May I get ur ur testapp ?
If u can forward it to me please forward it to
aniket0001@gmail.com
By silversurfer on Jul 14, 2008 | Reply
I could not find Class1 but replaced it with NUnit.Gui.AppEntry.Main