20

I have a problem debugging an NUnit test from VisualStudio. I created an empty project (Console Application), then I added references to the NUnit library and wrote a simple test.

namespace ReimplementingLinq.Tests
{
    [TestFixture]
    public class WhereTest
    {
        [Test]
        public void SimpleFiltering()
        {
            int[] source = { 1, 2, 3, 4, 2, 8, 1 };
            var result = source.Where(val => val < 4);
            int[] expected = {1,2,3,4};
            CollectionAssert.AreEqual(expected, result);
        }
    }
}

Next I followed the advice given in this link How do I run NUnit in debug mode from Visual Studio? but none of the solutions in that topic work for me. None of my breakpoints are hit while performing the test. I tried testing the solution by attaching to the process and also by running the project with an external program with arguments.

What can I do to debug my unit test?

Community
  • 1
  • 1
commando
  • 235
  • 1
  • 3
  • 6
  • Are you saying you're attaching to the nunit.exe process, run the tests in the nunit IDE but your breakpoints are not getting hit? – Martin Haluza Jul 24 '11 at 22:15
  • Is it possibly that NUnit is trying to run under a different framework version? Try the following links: http://stackoverflow.com/q/3542904/13188 and http://stackoverflow.com/q/930438/13188 – Pedro Jul 25 '11 at 03:35
  • I got it working with the answer I posted here: http://stackoverflow.com/questions/13348613/nunit-runners-via-nuget-on-visual-studio-2012-express-doesnt-work/16265575#16265575 – tobsen Apr 28 '13 at 17:23

5 Answers5

41

Running or debugging NUnit tests directly with Visual Studio

Look ma' no extensions!

Simply configure your test project so that when you hit F5 (Start debugging) or Ctrl-F5 (Start without debugging) it will automatically start NUnit GUI and execute all tests within it. If any breakpoints get hit, you will also be able to simply debug your test code.

A step-by-step guide with images shows you exactly how to do it.

Laziness is the mother of all invention :-)

Community
  • 1
  • 1
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
13

Assuming you're using a version of Visual Studio other than Express Edition then TestDriven.NET might be of use.

After installing it

  1. Set a breakpoint within your test method
  2. Right click and choose Debugger from the Test With menu
  3. The debugger should launch and hit your breakpoint

Unfortunately you can't use this method with Express editions of visual studio because TestDriven.NET is a plugin for visual studio and the Express editions do not support the use of plugins

Running a test within a console app

You can also run a test in the debugger via a console application:

  1. Create a new console application
  2. Reference your unit tests project
  3. Inside the Main method of the console application create a new instance of your test fixuture and then call one of the test methods. For example if I have a fixture named MyTests and a test named Test1 I'd write:

    var myTests = new MyTests();
    myTests.Test1();
    
  4. Set a breakpoint at the line where you create an instance of the MyTests class and press F5

  5. The debugger will hit your breakpoint and then you can use F11 to step into your TestFixture's constructor, or step over that into the test itself
Crippledsmurf
  • 3,982
  • 1
  • 31
  • 50
  • Thanks for answer, but is there maybe a free solution for my problem ? – commando Jul 24 '11 at 13:47
  • TestDriven.NET is free for personal use, it wasn't stated whether this was commercial or not, I'll edit and provide another solution that may work. – Crippledsmurf Jul 24 '11 at 13:50
  • -1 because it doesn't answer the question. The OP specifically asked about NUnit. – erict Feb 10 '13 at 23:18
  • 3
    @erict Could you please clarify your reasoning? The OP asks "What can I do to debug my unit test ???" Both methods discussed will result in an NUnit test executing in the Visual Studio debugger as per the question title. The tests themselves still use the NUnit framework as they normally would, so I'm uncertain as to what you mean because the solutions above do still use NUnit. – Crippledsmurf Feb 11 '13 at 19:58
  • @crippledsmurf My apologies. I only quickly looked at testdriven.net and thought it was a competing test package similar to NUnit. I had not realized that it is in fact a means to make it easier to use NUnit from within VS. Unfortunately I can't undo the -1, I hope the +1 I gave your query above this comment has the same effect. I did have the same problem as the OP, for me the solution was to force .net 4 in the config file (which I think is the proper answer) – erict Feb 13 '13 at 00:39
  • @erict No harm done :). I was really just confused as to the logic behind the down-vote which you've explained. – Crippledsmurf Feb 13 '13 at 02:13
5

If you are using NUnit 2.4 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.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
0

Have a look at NHarness on CodePlex.

It's a very simple, reflection based, test runner library, that recognises NUnit attributes, and can be run from a project within Visual Studio Express, allowing debug testing.

It currently has a test class level granularity, but method level calls are, supposedly, going to be added soon.

johnc
  • 39,385
  • 37
  • 101
  • 139
0

Had the same problem with NUnit 2.5.3, and eventually found a different solution. See if this works for you:

Open NUnit GUI and go into Tools menu Settings... dialog. Select Assembly Isolation subsection of Test Loader section in Settings tree. Set the Default Process Model to Run tests dierctly in the NUnit process.

I had it set to Run tests in a single seperate process, which is why the debugger could not link my dll under test to the symbols for debugging it. I am still using Use a sperate AppDomain per Assembly for my Default Domain Usage.

James
  • 41
  • 1