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

Viewed 98780

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.

19 Answers

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?

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

Install TestDriven.NET, which is a plugin for Visual Studio

From there you can right click on your unit test assembly and click Run Tests to run the whole suite, right click on a TestFixture class to run just the tests in that class, or right click on a Test method to run just that method.

You also have the option to Test With Debugger, if you need to breakpoint into your tests in debug mode.

Now with pictures:

  1. Run NUnit gui (Download 2.6.2 from here) then go to File -> Open Project

enter image description here

  1. Select your test .dll from bin folder (C:\......\[SolutionFolder][ProjectFolder]\bin\Debug\xxxxTests.dll)

  2. Go to Visual Studio Debug -> Attach to process (Attach to process window will open)

  3. From the list scroll down and select nunit-agent.exe then click Attach

enter image description here

  1. At this point breakpoints in your tests should turn ripe red (from hollow).

  2. Click Run on Nunit Gui and you should get your breakpoint hit...

Hope this saves you some time.

If project path contains spaces e.g. "New Project" in path <path>\bin\Debug\New Project\Quotes.Domain.Tests.dll then enclose the Start Option --> Command Line Arguments project path in double quotes.

I spent a lot of time to figure this out.

It sounds like you are trying to use the wrong library. NUnit can only start if the dll you are using contains TestFixtures.

+1 on TestDriven.Net. I've had the chance to use it a number of times. You can download the personal version for evaluations purposes according the the license at http://testdriven.net/purchase_licenses.aspx.

There is also an extension now "Visual NUnit" that will allow you to run the tests from within Visual studio much like the build in test framework handles. Check it out its in the extension manager.

See if this helps.. How to add NUnit in Visual Studio

(RighteousRant)Although personally I don't like this approach.. If you need a debugger while you are test-driving your code, it's a "smell" in that you do not have enough confidence/know how your code works & need the debugger to tell you that. TDD should free you from needing a debugger if done right. Use 'Attach debugger to NUNit' only for rare cases or when you are wading in someone else's code.

Related