Click here to Skip to main content
Click here to Skip to main content

Easy Debugging of NUnit Tests from Visual Studio 2008 Professional

By , 23 Jun 2009
 

Introduction

This article describes a convenient way of debugging NUnit tests using open source plugin to Visual Studio 2008. Many open source and cross platform projects choose NUnit testing framework for mono and nant support.

Background

Visual Nunit plugin for Visual Studio 2008 was developed to speed up development of MXP protocol reference implementation. Being able to debug unit tests easily from integrated development environment is a big efficiency factor.

Installation

  1. Download the latest release from here.
  2. Run the installer.
  3. Start Visual Studio 2008.
  4. Select from Menu View->Other Windows->NUnit View.
  5. Place the view to location of your choice.

Debugging Process

The debugging with Visual Nunit is achieved by simply double clicking the test at NUnit View. To create test cases to your project, do the following steps:

  1. Download NUnit 2.5 from here.
  2. Add nunit.framework.dll to your projects references.
  3. Add test case to the project (See example below).
  4. Make sure your project dependencies have copy local properties set.
  5. Build the project.
  6. To start debugging, click the arrow button of a test row in the NUnit View.
  7. Proceed as normal with Visual Studio Debugging.

Stopping debug will terminate the nunit runner process if your unit test is hanging or if you do not wish to wait until it is completed.

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using System.Diagnostics;
using System.Threading;

namespace NunitTests
{
    [TestFixture]
    public class ExampleTestFixture
    {
        [Test]
        public void TestSuccess()
        {
            Trace.TraceInformation("Test trace.");
        }

        [Test]
        public void TestException()
        {
            throw new Exception("Test Exception");
        }

        [Test]
        public void TestAssert()
        {
            Assert.Fail("Test Assert");
        }

        [Test]
        public void TestHang()
        {
            Thread.Sleep(10000);
        }
    }
}

Points of Interest

Visual Nunit was written with Microsoft Visual Studio 2008 SDK. The project type is Visual Studio Integration Package and the sources are available as open source project from here.

History

  • 21st June 2009 - Initial version

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0

About the Author

Tommi Laukkanen
Architect
Finland Finland
Web and rich client solutions since 2002 with .NET and Java.
 
Open source contributor to Visual Nunit, OpenSimulator, MXP protocol and IdealistViewer.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questioncan't step through codemembergary-s14-Apr-12 9:55 
I can't seem to get the Visual NUnit runner to hit a breakpoint and let me step through code. I click the red arrow next to a test and visual studio seems like its building and starting debug mode - and then it finishes as if no breakpoints were hit and Visual NUnit displays the test result.
 
Using Visual Studio 2010 and Visual NUnit 1.2.4.
QuestionRe: can't step through codememberAndreas Gieriet26-Dec-12 12:20 
Hello gary-s1,
I observe the same in my setup. Any solution for this?
Cheers
Andi
QuestionVery nice tool! just one questionmemberpaul.vencill5-May-11 5:34 
So, I really like your tool, thanks.
 
My question is, is there a way to do keybindings or build tasks for it? I'd like to either run/debug tests with a key combo (like I can w/ MSTest, e.g. CTRL+R,T), or else be able to just have them automatically run or debug whenever my test project does. Any way for me to set that up?
 
Thanks again, awesome work.
Generalvisual NUnit can't find unit testsmemberx-coder200710-Jan-11 1:09 
hello
It's the first time I'm using visual NUnit. And it seams that it can't find my test. I can select the project but nothing appears in the namespace drop down nor in the tests drop down.
here is the code doe C#//
<pre>namespace C_sharp__test_for_visual_NUnit
{
public class Class1
{
public void f()
{
 
}
}
}</pre>
<pre>namespace TestProject1
{
[TestClass]
public class UnitTest1
{
public UnitTest1()
{
}
 
private TestContext testContextInstance;
 
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
[TestMethod]
public void TestMethod1()
{
}
}
}</pre>
 
and here is the code from managed c++
 
<pre>namespace NunitTests
{
[TestFixture]
ref class ExampleTestFixture
{
public:
[Test]
void TestSuccess()
{
Assert::AreEqual(1,2);
}
 
[Test]
void TestException()
{
throw gcnew Exception("Test Exception");
}
 
[Test]
void TestAssert()
{
Assert::Fail("Test Assert");
}
 
[Test]
void TestHang()
{
Thread::Sleep(10000);
}
};
}</pre>
GeneralCan't run tests - please helpmemberbszom28-Oct-10 5:40 
Hi Tommi,
 
When I try to run my tests through the Visual Nunit window they instantly fail. Here are the exception details:
 
Failure: System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
----> System.NullReferenceException : Object reference not set to an instance of an object.
 
Stack Trace:
System.RuntimeMethodHandle._InvokeConstructor(IRuntimeMethodInfo method, Object[] args, SignatureStruct& signature, RuntimeType declaringType)
System.RuntimeMethodHandle.InvokeConstructor(IRuntimeMethodInfo method, Object[] args, SignatureStruct signature, RuntimeType declaringType)
System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
NUnit.Core.Reflect.Construct(Type type)
NUnit.Core.TestMethod.Run(TestResult testResult)

 
This is with Visual Studio 2010 Pro, NUnit 2.5.8, Visual Nunit 1.1.7 (as reported by VS Extension Manager, which is how I installed it - your SourceForge page lists the latest version as 1.1.3 while the download button on the summary page links to 1.0.3 ???)
 
The references for my test project (that's also the project type) are all set to copy local, except for all of the refs to the System namespace and Microsoft.VisualStudio.QualityTools.UnitTestFramework. I've tried setting copy local to true on Microsoft.VisualStudio.QualityTools.UnitTestFramework, without success. nunit.framework.dll is definitely being copied.
 
How can I figure out what's going wrong?
GeneralRe: Can't run tests - please helpmembernaughton8-Aug-12 14:28 
I am using VS 2008 and Visual NUnit has been a part of my solution for a long time now. Today I did an "unload" of one of my projects in the solution (it happened to be the Startup project) and then NUnit started acting funny - it was not showing my tests any more, and then would throw the exception named above.
The fix at this point was just to reload the project I had unloaded, then Visual NUnit worked fine. (There may be a better explanation but hopefully this will help if you have the same situation.)
GeneralVisual Nunit 2010 1.1.7 ReleasedmemberTommi Laukkanen9-Oct-10 21:55 
Released Visual Nunit 2010 bug fix release to remove crashes related to viewing empty test messages.
 
-Tommi
GeneralRe: Visual Nunit 2010 1.1.7 ReleasedmemberScythal31-Dec-10 5:41 
Last release on Sourceforge is 1.1.3: https://sourceforge.net/projects/visualnunit/files/VisualNunit%202010/[^]
 
Care to provide a link? Or am I missing something?
 
Thanks!
GeneralStopping at breakpointsmemberMember 292366017-Aug-10 15:14 
I put a breakpoint in the method that is being called by the test project (i.e the thing being tested). However that breakpoint is not hit when I run Visual NUnit tests? Why?
GeneralRe: Stopping at breakpointsmemberTommi Laukkanen9-Oct-10 21:53 
Please use the red arrow in the row to start the test in debug mode.
GeneralDebugging of NUnit tests:VS 2010, Visual NUnit and project's config filememberlevenyu16-Aug-10 12:02 
Hi folks!

I have installed VisualNunit_2010_1_1_6.vsix, which provided me with the Visual NUnit v. 1.1.6 extension for VS 2010.
Visual NUnit v. 1.1.6 runs and allows me to debug the test and other code, but there is an issue.
For example, the following test statement fails by throwing NullReferenceException (Object reference not set to an instance of an object.) when I try to debug my test code (using debug F10 key in particular):
Assert.IsInstanceOf<String>(ConfigurationManager.ConnectionStrings["MyTestConnInfo"].ConnectionString, "Assert.IsInstanceOf<String>");
 
It happens because Visual NUnit does not see my project's config file (and I have just one project under my Web application solution) and therefore ConfigurationManager.ConnectionStrings collection works with machine.config file which is the only one found at that moment.
Please note that I do not have any problem related to the use/read of project's config file(s) when I use NUnit 2.5.7 GUI application and when I attach my tests to that running process. For that I copied my web.config file as MyTestProject.config file which matches MyTestProject.cproj file by name.
So, how can I make the Visual NUnit to find and read configuration key values from the project's config file(s) under VS 2010? E.g. Web.config file located at the root of my web application?
 
Please advise.
 
Thank you,
levenyu
GeneralRe: Debugging of NUnit tests:VS 2010, Visual NUnit and project's config filememberSRM196929-Sep-10 17:45 
Hi,
 
Did anyone sort this issue out? I'm getting the same problem with ConfigurationManager.AppSettings["XXXX"] in a test case.
Confused | :confused:
 
Cheers
SRM
GeneralRe: Debugging of NUnit tests:VS 2010, Visual NUnit and project's config filememberTommi Laukkanen29-Sep-10 18:18 
As a workaround you can place VisualNunitRunner.exe.config to the project root.
QuestionCould you make a VS 2010 version please?mvpOmar Al Zabir5-May-10 5:12 
Could you make a VS 2010 version please?
(regards) => "Omar AL Zabir"
+ "C#, ASP.NET MVP"
+ "http://omaralzabir.com";

AnswerRe: Could you make a VS 2010 version please?memberTommi Laukkanen5-May-10 5:47 
Hi
 
You can find Visual Nunit from Visual Studio 2010 addons gallery.
 
Regards,
Tommi
GeneralVisual Nunit 2010 1.1 and Visual Nunit 2008 1.0.2 releasedmemberTommi Laukkanen19-Dec-09 4:20 
Hi
 
Visual Nunit is now released for Visual Studio 2010 with .NET 4.0 support. It can be installed using the Online Gallery from Visual Studio Extension Manager.
 
Visual Nunit 2008 version received also bug fix release.
 
-tommi
QuestionTests not showing up?memberBernard O'Flynn26-Nov-09 23:36 
I can run my web tests with Resharper, but not Visual NUnit? It sees the assembly in the drop down list, but when I click on it, no tests show up?
I'm trying to use it to run web tests with selenium.
AnswerRe: Tests not showing up?memberDan Neely7-Jan-10 5:18 
Bernard O'Flynn wrote:
It sees the assembly in the drop down list, but when I click on it, no tests show up?

 

 
I'm having the same problem; did you ever find a solution?
 
3x12=36
2x12=24
1x12=12
0x12=18

AnswerRe: Tests not showing up?memberjfh1089-Jan-10 7:13 
Same here - I can select the assembly in the "Project" dropdown, but the "Namespace" and "Fixture" dropdowns are empty. Tried typing in the values directly with no luck.
AnswerRe: Tests not showing up?memberom29721-Jan-10 23:59 
Exactly the same here - did anyone solve this issue? I'm just running the test provided in Visual Studio 2008 Standard edition.... Can only select the Project, nothing shows in the Namespace or Fixture dropdowns and none of the tests show in the Visual NUnit window. Any help would be greatly appreciated..... Thanks
GeneralBeta Release of Visual Nunit support for Visual Studio 2010memberTommi Laukkanen6-Nov-09 17:21 
Visual Nunit 2010 Beta has been released. It is directed to visual studio 2010 Beta 2. This beta version does not yet support .NET 4.0 as NUnit is currently lacking .NET 4.0 support.
 
-tommi
GeneralVisual Nunit 1.0.1 ReleasedmemberTommi Laukkanen1-Nov-09 6:53 
This is a bug fix release.
 
Bugs fixed:
 
2832709 If Copy Local is false then visual nunit crashes
2833764 VisualNunitRunner stops working with Silverlight 3.0 project
2854361 x86 project on x64 windows
 
-tommi
GeneralVisual Nunit Runner stopped workingmembertomasz pr.30-Jul-09 1:26 
After loading solution, there is an exception 'Visual Nunit Runner' stopped working' and it's impossible to use this plugin.
I'm running VS 2008 SP1 on Vista x64.
Is there any log somewhere which will help you to solve this issue?
GeneralRe: Visual Nunit Runner stopped workingmemberTommi Laukkanen30-Jul-09 20:11 
Hi
 
Better login support has to be added to the plugin but currently the only way to debug this issue would be you to install Visual Studio 2008 SDK and download the sources. Alternatively you could send me a solution which I can use to reproduce the problem.
 
I am using Windows Vista 64 myself so its more likely to be an issue related to some difference in solution content.
 
-Tommi
GeneralRe: Visual Nunit Runner stopped workingmembertomasz pr.30-Jul-09 21:56 
My solution contains a lot of different types of projects. There is MVC webapp, Silverligt 3.0 and a few plain libraries.
I suspect that Silverlight can be a problem. Can you investigate it or recommend a workaround?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 24 Jun 2009
Article Copyright 2009 by Tommi Laukkanen
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid