Skip to content

HOW TO Debug a test

Scott Hutchinson edited this page Dec 5, 2019 · 5 revisions

One way is to attach the debugger to a running dotnet test.

Call the following function at the beginning of the test you want to debug to wait on the debugger:

let debugBreak () = 
    if not System.Diagnostics.Debugger.IsAttached then
      printfn "Please attach a debugger, PID: %d" (System.Diagnostics.Process.GetCurrentProcess().Id)
    while not System.Diagnostics.Debugger.IsAttached do
      System.Threading.Thread.Sleep(100)
    System.Diagnostics.Debugger.Break()

Run the tests, usually with dotnet test, who will wait until the debugger is attached

Now in VSCode in the Debug pane on the left, select the configuration .NET Core Attach, and click the Start Debugging button.

(If you don't already have the configuration .NET Core Attach, then add it. Let IntelliSense help you add it to the launch.json file. It should look like this:

    {
        "name": ".NET Core Attach",
        "type": "coreclr",
        "request": "attach",
        "processId": "${command:pickProcess}"
    },

)

Select the test process, and the debugger should attach and break where you put the Debugger.Break call. If you can't find the Process ID in the console window, then try selecting the process that looks like "dotnet.exe exec --runtimeconfig ...".

NOTE: You can filter the test to run with dotnet test --filter "TestName"

NOTE: See the complete list of tests with dotnet test --list-tests

Clone this wiki locally