How do I run NUnit in debug mode from Visual Studio?

97,161

Solution 1

I use the same technique as you are trying Jon, without the /assembly flag, i.e.

Start External Program: C:\Program Files\NUnit 2.4.8\bin\nunit.exe

Command line arguments: "<path>\bin\Debug\Quotes.Domain.Tests.dll"

Does TestDSP.dll contain all your TestFixtures?

As my test project is not the startup project in the solution, I run my tests by right-clicking on the test project and choosing Debug --> Start New Instance

Solution 2

When I need to debug my NUnit tests, I simply attach to the NUnit GUI application nunit-agent.exe using "Debug|Attach to Process" and run the tests from the GUI. Any breakpoints in my tests (or the code they're testing) are hit. Am I misunderstanding your question, or will that work for you?

Solution 3

Simply remove the line that looks like

<ProjectTypeGuids>
    {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
</ProjectTypeGuids>

from your project file. This line basically tells VS.Net that it's a Test project, thus the "Cannot start test project". FYI here the 1st Guid says "it's a test", the 2nd says "it's C#". For information on those Guids: http://www.mztools.com/Articles/2008/MZ2008017.aspx

Solution 4

If you are using NUnit 2.4 or newer you can put the following code in your SetUpFixture class. (You can do this with older versions but you will need to do whatever equivalent that has to the SetUpFixture, or copy it in to the test itself.)

[SetUpFixture]
public class SetupFixtureClass
{
    [SetUp]
    public void StartTesting()
    {
        System.Diagnostics.Debugger.Launch();
    }
}

What Debugger.Launch() does is cause the following dialog to show up when you click Run inside NUnit.

JIT Debugger Dialog

You then choose your running instance of visual studio with your project open (the 2nd one in my screenshot) then the debugger will be attached and any breakpoints or exceptions will show up in Visual Studio.

Solution 5

In addition to the answer provided by @Justin here are some more details for NUnit 2.6.

Using NUnit 2.6 attach to nunit.exe or nunit-console.exe and NOT the agent. The configuration noted by @Justin is slightly different. Below is an example from nunit.exe.config (same for nunit-console.exe.config).

<startup useLegacyV2RuntimeActivationPolicy="true">
  <!-- Comment out the next line to force use of .NET 4.0 -->
  <supportedRuntime version="v2.0.50727" />  
  <supportedRuntime version="v4.0.30319" />
</startup>

For .NET 4 test project, to get break points to hit, you will have to comment out or remove the v2.0 line as the comment suggests. Once I did that I was able to debug the .NET 4.0 test project.

Share:
97,161
Jon Cage
Author by

Jon Cage

A Computer Systems Engineer based in the UK

Updated on January 23, 2020

Comments

  • Jon Cage
    Jon Cage over 4 years

    I've recently been building a test framework for a bit of C# I've been working on. I have NUnit set up and a new project within my workspace to test the component. All works well if I load up my unit tests from Nunit (v2.4), but I've got to the point where it would be really useful to run in debug mode and set some break points.

    I've tried the suggestions from several guides which all suggest changing the 'Debug' properties of the test project:

    Start external program: C:\Program Files\NUnit 2.4.8\bin\nunit-console.exe
    Command line arguments: /assembly: <full-path-to-solution>\TestDSP\bin\Debug\TestDSP.dll
    

    I'm using the console version there, but have tried the calling the GUI as well. Both give me the same error when I try and start debugging:

    Cannot start test project 'TestDSP' because the project does not contain any tests.
    

    Is this because I normally load \DSP.nunit into the Nunit GUI and that's where the tests are held?

    I'm beginning to think the problem may be that VS wants to run it's own test framework and that's why it's failing to find the NUnit tests?

    Edit: To those asking about test fixtures, one of my .cs files in the TestDSP project looks roughly like this:

    namespace Some.TestNamespace
    {
        // Testing framework includes
        using NUnit.Framework;
    
        [TestFixture]
        public class FirFilterTest
        {
            [Test]
            public void Test01_ConstructorTest()
            {
                ...some tests...
            }
        }
    }
    

    ...I'm pretty new to C# and the NUnit test framework so it's entirely possible I've missed some crucial bit of information ;-)

    Final Solution: The big problem was the project I'd used. If you pick Other Languages -> Visual C# -> Test -> Test Project ...when you're choosing the project type, Visual Studio will try and use it's own testing framework as far as I can tell. You should pick a normal C# class library project instead and then the instructions in my selected answer will work.

  • Jon Cage
    Jon Cage about 15 years
    I just tried and that does indeed work. If Patrick's answer is a no-go I'll accept this as a workable solution :-)
  • Jon Cage
    Jon Cage about 15 years
    I've tried what you've suggested (removing the /assembly) but it makes no difference. When I do start new instance it produces the error. I'm thinking it's mostly to do with the fact that when I created the TestDSP project I created it from the built in VisualStudio test project template so it's looking for the wrong testing framework.
  • Jon Cage
    Jon Cage about 15 years
    That's pretty good although I can't find a way to tell it to just run a single test(?)
  • Jon Cage
    Jon Cage about 15 years
    I tried the suggestions there to no avail. You clearly have a good nose. I know my code doesn't work as the output I'm getting for the first block of implementation is getting wildly different answers to my test reference. So now I'm trying to delve deeper to find the cause of the issue. I'd rather do that in isolation to the rest of the program (hence the need to run unit tests in debug mode). For the record, this is code someone else wrote that was converted from another persons algorithm :-/
  • Jon Cage
    Jon Cage about 15 years
    See the recent edit - I have a test fixture although it's entirely possible I've not set it up correctly.
  • Gishu
    Gishu about 15 years
    So that falls into the latter clause of my last line :) Strange that you can't get it to work though.. too bad. I'd say just attach to process (Alt+D+P) without dwelling on it..
  • Patrick McDonald
    Patrick McDonald about 15 years
    How big is your project? Could you create a new empty project, add a reference to NUnit and add in all your TestFixture classes? If you created the project using the Visual Studio template, can you not use the Visual Studio test runner to run your tests?
  • Jon Cage
    Jon Cage about 15 years
    Finally got it working. I was right in so much as it was the project options that was stopping it - re-creating the test project using the standard class template fixed the issue.
  • Richard
    Richard almost 13 years
    For your (and others') information: the Debug|Attach is not available in the Express editions of VS.
  • Julio Garcia
    Julio Garcia over 12 years
    Be aware that you have to select "Enable Visual Studio Support" on the Settings dialog of NUnit -> IDE Support
  • Robert Koritnik
    Robert Koritnik over 12 years
    It helps if you also add /run to your *Command line arguments which will automatically start executing tests... I've also summed it all up (using images) in my blog post.
  • devuxer
    devuxer over 12 years
    For .NET 4.0 and later, I believe you also have to add this to nunit.exe.config: <startup> <supportedRuntime version="4.0" /> </startup>.
  • devuxer
    devuxer over 12 years
    In case people don't check out Robert's (very useful) blog post (erraticdev.blogspot.com/2012/01/…): for .NET 4.0 and later, I believe you also have to add this to nunit.exe.config: <startup> <supportedRuntime version="4.0" /> </startup>.
  • Ben Hardy
    Ben Hardy about 12 years
    $170 is ridiculously steep for such a tool. Price gouging, anyone?
  • devuxer
    devuxer almost 12 years
    Followup: In later versions of NUnit (the latest version as of today is v2.6.1), you need to comment out <supportedRuntime version="v2.0.50727" /> in nunit.exe.config.
  • Roman
    Roman over 11 years
    Yeah. For that kind of money, I'd rather invest in JetBrains Resharper which then gives the Test Runner for free with debugging integration and a bunch of other productivity features.
  • Jon Limjap
    Jon Limjap over 11 years
    With Visual Studio 2012 you can get the NUnit Test Runner with Nuget for free.
  • gmail user
    gmail user over 11 years
    Awesome.. Made my day. Simple and accurate.
  • bart
    bart over 11 years
    This is a quick shortcut for attaching to the correct process (run in Package Manager Console): ($dte.Debugger.LocalProcesses | ? { $_.Name.EndsWith("nunit-agent.exe") }).Attach()
  • Ricardo stands with Ukraine
    Ricardo stands with Ukraine about 11 years
    For me it only works when attach debugger to NEGui.exe and nunit-agent.exe processes at the same time. No other changes mentioned in previous comments required (NUnit version 2.6.1)
  • Krisztián Balla
    Krisztián Balla about 10 years
    FYI: you need to attach debugging to the process called "nunit-agent.exe" and NOT "nunit.exe". Otherwise your breakpoints are ignored and you wonder why...
  • BrainSlugs83
    BrainSlugs83 over 9 years
    There is no smell here -- I have a test case that fails in certain environments (a very wrong result is returned), and I need to figure out why. To do that, I want to debug it and find out where its failing in this environment so that I can fix the code and make the test pass everywhere. This seems like standard red/green type stuff ...
  • Gishu
    Gishu over 9 years
    @BrainSlugs83 - long time since I wrote this. I am (still) against debugging your tests as a primary work practice. Edge cases - I'm okay with dropping to the debugger. Even then I'd probably insert logging stmts first.. I think it stems from the fact that I've observed too many people using a Code-Crash-Debug-Adjust cycle which gets streamlined to Code-Crash-Adjust cycle with the debugger continuously on.
  • MarkJ
    MarkJ over 8 years
    When I need to debug or run my NUnit tests I just press F5 the same way I run or debug any code. It doesn't get any simpler than that.
  • Martin Ba
    Martin Ba almost 8 years
    I had sucess with only the v2.0.50727 line when debugging .NET 2 assemblies from VS2005 with nunit. (The v4 line prevented the debugger of VS 2005 from attaching.)
  • dano
    dano over 6 years
    Thanks for this answer. Much simpler than all the others.
  • mggSoft
    mggSoft over 5 years
    I'm using NUnit 2.5.9 in VS 2015 and it worked with a VS extension named as 'NUnit 2 Test Adapter'. You can execute test on the Test Explorer window.