13

I have a .NET 4.0 C# solution with a tests project which runs unit tests under NUnit. The NUnit binaries are v3.5.

I can run the tests perfectly well, but I can't set breakpoints and single step in Visual Studio. I'm guessing this is caused by the mismatch in .NET versions. Is there a way to single step through a v4.0 tests assembly using NUnit for v3.5?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
simonc
  • 41,632
  • 12
  • 85
  • 103
  • You should be able to set breakpoints in this case. How are you running the tests, and how are you attaching the debugger? – Frank Schwieterman Feb 15 '12 at 17:16
  • @FrankSchwieterman The Debug properties of my project have a start action which points to nunit-console.exe and command line args containing the nunit args. I'm setting breakpoints then selecting Debug in solution explorer for the tests csproj. The debugger runs and the status of the breakpoint changes with the error "symbols cannot be loaded" – simonc Feb 15 '12 at 17:24
  • hmm it doesn't sound like visual studio has attached to the process running your tests (it will be attached to whatever application you hit f5 for, though that app has a build step to launch NUNit) – Frank Schwieterman Feb 15 '12 at 17:48
  • 1
    if you run the NUnit GUI app outside of VS, you can load your test DLL then attach to the process from visual studio. (In the debug menu, click "attach to process", then find the nunit process) – Frank Schwieterman Feb 15 '12 at 17:49
  • @FrankSchwieterman thanks for the suggestion but visual studio still failed to load debug symbols (from a pdb in the same location as the test assembly it had found) – simonc Feb 15 '12 at 17:57
  • Workaround: [print debugging](https://en.wikipedia.org/wiki/Debugging#Techniques) (using `TestContext.WriteLine()`). Ref: *[How do I get nunit3-console to output my debug to screen?](https://stackoverflow.com/questions/46405753/how-do-i-get-nunit3-console-to-output-my-debug-to-screen-on-a-windows-box)* - *"in NUnit 3, you should use TestContext.WriteLine(...) in your tests"*. It [works on Linux](https://stackoverflow.com/questions/46405753/how-do-i-get-nunit3-console-to-output-my-debug-to-screen-on-a-windows-box#comment110657051_46407376) as well. – Peter Mortensen Jul 10 '23 at 09:28

5 Answers5

28

The problem is that unless you tell it otherwise, NUnit will spawn a subprocess to run tests when it determines it necessary. If you watch it in Process Explorer, you can see that "nunit-console.exe"* spawns "nunit-agent.exe"*. The Visual Studio debugger doesn't automatically attach to child processes.

In this case, I believe the version mismatch is why it chooses to start a subprocess. The easiest way to work around this is to edit "nunit-console.exe.config"* to change the set of <supportedRuntime> values. There should already be a comment there marking the line that you should comment out in order to force it to run as .NET 4.0:

<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>

Once you change that, the first NUnit process will already be .NET 4.0 and it shouldn't need to spawn a subprocess. If you want to be sure, specify /process=Single and NUnit will either run in a single process or fail immediately if it cannot.

* - If you need to use the x86 versions, substitute:

nunit-console.exe        -> nunit-console-x86.exe
nunit-agent.exe          -> nunit-agent-x86.exe
nunit-console.exe.config -> nunit-console-x86.exe.config
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Weeble
  • 17,058
  • 3
  • 60
  • 75
  • Awesome, thanks. I had seen you were supposed to add the supportedRuntime elements, which hadn't worked. I hadn't noticed I was using the x86 version and that that had its own config file. – neminem Mar 29 '13 at 14:59
  • @bmargulies - I don't believe that would help, because by the time NUnit is parsing its arguments, it's too late - it has already started as a .NET 2.0 process and will need to spawn a subprocess to meet your request for a .NET 4.0 process. – Weeble Jul 11 '13 at 08:27
  • Do we have to recompile it to get it to run as 4.0? – bmargulies Jul 11 '13 at 15:52
  • No, that's what editing the config file does - it is read by .NET before it decides which runtime to use. – Weeble Jul 11 '13 at 20:23
  • Thank you, this solved the problem! I took a slightly different route by [piggy-backing on top of NUnit console runner](http://pastebin.com/Ay8VCHSc) while injecting the necessary command-line options into it to make it use the single process. To properly deploy all the NUnit's stuff to the output directory, [two tweaks](http://pastebin.com/VmSSAJJ9) are also needed in the runner's project file. – kostix Sep 03 '15 at 18:15
  • note the config option for single process has changed with NUnit 3.0 to --process=InProcess – Ceilingfish Jul 18 '16 at 15:00
9

My answer is for another entire version of NUnit. However, for somebody like me, that is just discovering this I installed NUnit and NUnit Console via Manage NuGet Packages... (1st and 3rd option in the screenshot).

Enter image description here

And so I configured my test project properties Debug tab (see next screen shot below) in Visual Studio 2015 Community edition to run nunit3-console.exe which is found off your <solution>\packages folder that is automatically created when you install "NUnit Console" and for the arguments I added my test library DLL file and the commandline switches --wait (which prompts the developer to "Press any Key to Close", so it allows you see the result), and more importantly --inprocess that attaches your test library .NET code automatically so your break points are hit.

Enter image description here

Note to run the NUnit 3 console application, you set your test project as the start-up project.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Glen
  • 802
  • 1
  • 11
  • 27
3

Another option is to use http://testdriven.net/ to run your tests through Visual Studio. You can put a breakpoint on a test and right click → Run testsWith debugger.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Trevor Pilley
  • 16,156
  • 5
  • 44
  • 60
2

ReSharper allows you to step through your Unit tests while debugging. But I don’t think you can do the same with Visual Studio. Try installing the trial version of ReSharper and then try to debug the tests.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gprasant
  • 15,589
  • 9
  • 43
  • 57
  • Resharper sometimes doesn't do well either - when you're trying to make a single step it breaks through right to next breakpoint. Some times that happens. Don't know why =) – Sergey Savenko Feb 15 '12 at 17:51
  • Thanks, that works nicely. I'm sure I used to be able to do this with Visual Studio as well but running via ReSharper works too! – simonc Feb 15 '12 at 17:55
  • If you want to use Visual Studio without Resharper, you will be better off if you use MSTest instead of NUnit. Differences in syntax are pretty minimal. – gprasant Feb 15 '12 at 17:58
2

I'm note sure about the console application, but you should find you can start the GUI version of NUnit manually and then attach to the nunit-agent process from the debugger in Visual Studio.

Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
  • Thanks, do you know if this is still available in nunit 2.6? I can see code for nunit-gui but can't see an exe in the prebuilt binaries – simonc Feb 15 '12 at 18:03
  • 1
    *nunit.exe* is the GUI version. *nunit-console.exe* is the console version. Running nunit.exe will spawn the nunit-agent process. – Matthew Strawbridge Feb 15 '12 at 20:02