Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging MSTest Unittests in Visual Studio Code

I am trying to use Visual Studio Code to Debug a MSTest unit test project. But the tests just run and the breakpoint is never reached.

Here is my launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Test (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "C:\\Program Files\\dotnet\\dotnet.exe",
            "args": ["test"],
            "cwd": "${workspaceRoot}",
            "console": "internalConsole",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart"
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}

How can I debug a unit test (MSTest)? This same problem exists for XUnit.

like image 645
PWFraley Avatar asked Apr 04 '17 14:04

PWFraley


People also ask

How do I debug MSTest 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 multiple files in Visual Studio code?

To debug multiple services at the same timeOpen the folder corresponding to your first service in VS Code. In VS Code, select File > Add Folder to Workspace…, and pick the folder corresponding to your other service.

How do you debug a breakpoint in Visual Studio code?

To set a breakpoint in source code: Click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint.


2 Answers

If you are using the latest version of VS Code (I'm using v1.29.0), debugging unit test is in-built feature.

enter image description here

You need to first build the solution dotnet build for the test run & debug options to appear.

like image 152
Arghya C Avatar answered Sep 17 '22 20:09

Arghya C


Try https://github.com/Microsoft/vstest-docs/blob/master/docs/diagnose.md#debug-test-platform-components (assumes you're using dotnet-cli tools 1.0.0)

> set VSTEST_HOST_DEBUG=1
> dotnet test
# Process will wait for attach
# Set breakpoint in vscode
# Use the NETCore attach config from vscode and pick the dotnet process
like image 23
Arun M Avatar answered Sep 17 '22 20:09

Arun M