Search code examples
c#visual-studionunitnunit-consoledotnet-test

Debug NUnit dotnet test run from VS Debugger, not Test Explorer


TL;DR Is there an equivalent dotnet test switch to NUnit's --inprocess?

Because running dotnet test as StartProgram with the normal VS Debuigging experience (F5) does not allow to debug the test.


Our tests are written in NUnit 3. We are currently migrating our project from .NET Framework 4.8 to .NET 6.

Since running tests via nunit3-console.exe does not always work (e.g. https://github.com/nunit/nunit-console/issues/1253) we are also migrating the runner to dotnet test which currently works more seamlessly for us.

However, our devs used to be free to debug their tests how they wanted. Some used the VS Test Explorer, others used the normal VS debugger and "F5" with startup settings approx. nunit3-console.exe --inprocess some.test.dll.

The problem now is that I cannot find any way to debug an NUnit test started with <StartProgram>dotnet test from within the VS GUI.

I found: How to debug dotnet test in VS Code? which suggests setting VSTEST_HOST_DEBUG=1, but (a) you cannot set env vars with csproj StartProgram and (b) it doesn't seem to work with NUnit anyway.

I want to:

  • Set a breakpoint in my NUnit 3 test code
  • Run all the tests as a "regular" program from the Visual Studio debugger "F5"
  • break into the program on my breakpoints

I could do this with nunit-console.exe --inprocess -- is this possible with dotnet test at all?


Solution

  • Allegedly, this is not possible with dotnet test:

    Unfortunately, even if dotnet test runs in-process, it will spawn a testhost.exe to run your tests. Setting VSTEST_HOST_DEBUG simply tells testhost.exe to wait for a debugger to be manually connected. - NPras

    So the situation is similar to what NUnit (console runner) offers/offered with the --debug switch.