Debugging MSTest Unittests in VisualStudio Code

2019-02-25 09:28发布

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.

2条回答
混吃等死
2楼-- · 2019-02-25 10:23

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
查看更多
乱世女痞
3楼-- · 2019-02-25 10:30

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.

查看更多
登录 后发表回答