Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug dotnet test in VS Code?

This article describes setting the VS Code settings to point the debugging target at the build output of the unit test project. I have therefore set mine like this:

{
    "explorer.confirmDragAndDrop": false,
    "git.allowForcePush": true,
    "git.autofetch": true,
    "window.zoomLevel": 0,
    "csharp.unitTestDebuggingOptions": {
        "sourceFileMap": {
            "C:\\git\\MsTester\\bin\\Debug\\netcoreapp2.1": "C:\\git\\MsTester\\bin\\Debug\\netcoreapp2.1"
        }
    },
    "files.autoSave": "afterDelay",
    "files.exclude": {
        "**/bin": true,
        "**/node_modules": true,
        "**/obj": true
    },
    "csharpfixformat.style.spaces.insideEmptyBraces": false,
    "csharpfixformat.style.braces.allowInlines": false,
    "csharpfixformat.style.spaces.beforeParenthesis": false,
    "csharpfixformat.style.spaces.afterParenthesis": false,
    "csharp.format.enable": false,
    "extensions.ignoreRecommendations": true
}

However, I am not sure how to setup the launch.json to kick off the dotnet test so that it can attach the debugger.

This is what I've got currently:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "MsTester",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceFolder}/MsTester/bin/Debug/netcoreapp2.1/MsTester.dll",
            "windows": {
                "args": [
                    "--filter",
                    "TestCategory=lbshell",
                    "--logger",
                    "trx",
                    "--results-directory",
                    ".\\TestResults",
                    "--settings",
                    ".\\Features\\runsettings.xml"
                ],
            },
            "cwd": "${workspaceFolder}/MsTester",
            "console": "internalConsole",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart"
        },
    ]
}

Is there an option to tell VS Code that it needs to execute dotnet test instead of dotnet run?

I was hoping this page would indicate how to do that, but it does not.

like image 219
Matt W Avatar asked May 24 '19 09:05

Matt W


People also ask

How do I debug .NET code in Visual Studio Code?

Open the Debug view by selecting the Debugging icon on the left side menu. Select the green arrow at the top of the pane, next to . NET Core Launch (console). Other ways to start the program in debugging mode are by pressing F5 or choosing Run > Start Debugging from the menu.

How do I debug a .NET application in Visual Studio?

As the following image shows, Visual Studio indicates the line on which the breakpoint is set by highlighting it and displaying a red dot in the left margin. Press F5 to run the program in Debug mode. Another way to start debugging is by choosing Debug > Start Debugging from the menu.

How do I debug a test in Visual Studio?

To start debugging: In the Visual Studio editor, set a breakpoint in one or more test methods that you want to debug. Because test methods can run in any order, set breakpoints in all the test methods that you want to debug. In Test Explorer, select the test method(s) and then choose Debug on the right-click menu.

How do I debug a .NET project?

Press Ctrl-Shift-P / Cmd-Shift-P (all keyboard shortcuts in this guide are for macOS) and then search for the command . NET: Generate Assets for Build and Debug . This will have the same result as confirming the prompt: a . vscode directory will be added to your project.


1 Answers

All credit is due to @Lance U. Matthews as he posted this as a comment to an answer to the question: How does one debug an MSTest in VSCode? . I am reposting this as a proper answer because he hasn't done so in a year.

The following answer has been tested on VSCode v1.62.3 and dotnet tools v5.0.403.

Assuming you have a .NET solution with a structure like this:

mysolution.sln
tests\
  |---> tests.csproj
  |---> UnitTest1.cs
.vscode\
  |---> launch.json
  |---> tasks.json

Copy the following into the tasks.json file:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        { 
            "label": ".NET Core Test with debugger", 
            "type": "process", 
            "isBackground": true, 
            "command": "dotnet", 
            "args": [ "test" ], 
            "options": 
                { 
                    "cwd": "${workspaceFolder}/tests", 
                    "env": 
                    { 
                        "VSTEST_HOST_DEBUG": "1" 
                    }, 
                }, 
            "group": "test", "presentation": 
                { 
                    "echo": true,
                    "reveal": "always",
                    "focus": false,
                    "panel": "shared"
                },
            "problemMatcher": [] 
        },
    ]
}

And this into the launch.json file:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach"
        }
    ]
}

Now set a breakpoint anywhere in your UnitTest1.cs file and run .NET Core Test with debugger task (Terminal->Run task...). The Test Execution Command Line Tool should launch and at the bottom of your terminal should be something like: enter image description here Your Process Id will certainly be different but that is expected behavior.

Now you need to switch to "Run and Debug" tab (ctrl+shift+d) and select ".NET Core Attach" (it should be in the upper left part of the VSCode). Provide the process id that appeared previously in the terminal: enter image description here Now click F5 once and you should reach one of your breakpoints.

like image 195
zajer Avatar answered Sep 23 '22 03:09

zajer