13
votes

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?

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

5 Answers

27
votes

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

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.

3
votes

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.

2
votes

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.

2
votes

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.