The circle of life – ** .Net – Training – MOSS **

  • Past Post

  • Vistor Locations

Configuring NUnit in VS2005

Posted by Clayton James on October 28, 2006

While  there has been lots of talk about the benfits of unit tests and why they should be implemented in every developers project, I have seen very few development projects contain programatic unit test. I am not sure why this is but I am guessing it is mostly comes down to a few points; time contraints, understanding unit tests, percieved value,  or just getting started (configuration). This entry will focus on the “getting started”, and after you complete this most of the other points will fall into place.

VS2005 makes setting up a NUnit test project quite straight forward, and allows the developer to seemlessly create, visulalise and debug unit tests.

Software you need:
NUnit (current free version 2.2.8 win .net 2.0): provides the framework to create unit tests and run them in a GUI.
TestDriven.NET (personal edition is free): runs NUnit tests easily from Visual Studio IDE and outputs Asserts.

Test Project
Create a new class library project in your current solution. This project will contain all of your unit tests so provide an appropriate name. While were on the topic of naming check out SSW Rules on Unit Tests and naming conventions.

.config file issues
You may have an .config file already in your project making connections back to a database, but because NUnit loads its own executable in a seperate app domain it won’t be able to read the .config settings (unless you specify the path of the .config file in NUnits Project File). However, I am also using TestDriven.NET and from what I have seen doesn’t allow the same functionality, so to make the whole process alot easier you can add an app.config file to your UnitTest class library project and add an attribute to your Nunit Project File as shown below.

NUnit: If you open an NUnit Test Project File and click Project >> Edit, there’s an entry for Configuration File Name. In the textbox, I simply entered App.Config.

If you look at the .nunit file, it enters the config file as an attribute:

  <Settings activeconfig=”Debug” />
  <Config name=”Debug” configfile=”App.config” binpathtype=”Auto”>
    <assembly path=”bin\Debug\IPSClassLibrary.Tests.dll” />

Make your class and tests public
This is a trap for new players. You need to expose your class and methods publicly.

Write your test
After you have written your test inside Visual Studio you can easly test your unit test by right clicking inside the method and selecting Run Test(s) (this extra item is added when you installed TestDriven.NET….a must have). It is as simple as that.

NUnit has a GUI which is a nice display that easily informs the tester/developer if unit tests have passed or failed. This is easlily integrated into VS2005.

Debugging Unit Tests

POST UPDATE 7/12/2006
I received a comment from Peter Lillevold about an easier way to debug unit tests. Using Peter’s words:

If you’re using TestDriven.Net, to debug a unit test all you need to do is set a break point, right-click the test method, choose “Test with” and select Debugger.

Thanks for this Peter. Makes debugging unit tests much simpler.

Now you should be able to run your unit tests from Nunit or Visual Studio, so the next step is debugging. Again, our project is a class library so this causes issues.

As previously stated, the NUnit framework loads and runs the assemblies in separate AppDomain therefore you cannot use the debugging features of Visual Studio. However, you can attach any external program to VS to consume an assembly. Follow these steps:

  1. Set Start application to nunit-gui.exe that will be in bin folder of NUnit Framework folder.
  2. You can define the command line argument for nunit-gui.exe. Alternatively, you can first start nunit-gui.exe externally and open your test case assembly, run it once and save it. Nunit-gui.exe will persist the last open file and will open it when run from visual studio.

See the figure below: Note this is a VS2003 IDE but I configured mine in VS2005.

Now with your Test project as your Start Project and run up your solution. NUnit GUI should open automatically and you should now be able to debug using Visual Studio.

With this you can run and debug your application code with great features provided by the Visual Studio.


7 Responses to “Configuring NUnit in VS2005”

  1. Thabet said

    Thanks a lot for your short bur very usful articale.

    I have only one comment; As new C# programmer, I could not understand the steps in Debugging Unit Tests. For example; Set Start application to nunit-gui.exe …

    I will be very grateful if you can explain it a littel bit more.

    Thanks inadvance…

    We are using VS2005 in the University, on Windows XP pro .


  2. Hi!

    If you’re using TestDriven.Net, to debug a unit test all you need to do is set a break point, right-click the test method, choose “Test with” and select Debugger.

  3. CJ said

    Great point Peter. I didn’t even know TestDriven.Net had that option. I will update this post.

  4. bur said

    This article explains how to easily set up config files in Nunit.. No messing


  5. Chu Dinh Than said

    Please give me a example that you was configured NUnit in VS2005
    Many thanks

  6. The style of writing is quite familiar to me. Have you written guest posts for other bloggers?

  7. Spot on with this write-up, I honestly feel this website needs far mor
    attention. I’ll probably be returning to read more, thanks for thee advice!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: