如何在VSCode中调试MSTest?

10
在安装了C#扩展的VSCode 1.17.2版本中,我通过dotnet new mstest向解决方案文件夹添加了一个MSTest项目,并使用dotnet add <project_path>引用被测试的程序集。
通过下面这两个VSCode任务,我可以成功构建和运行测试;也就是说,所有内容都能构建,单元测试运行并且通过。
{
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "build",
            "command": "dotnet build src/tests/tests.csproj",
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal": "silent"
            },
            "problemMatcher": "$msCompile"
        },
        {
            "taskName": "test",
            "command": "dotnet test src/tests/tests.csproj",
            "type": "shell",
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "presentation": {
                "reveal": "silent"
            },
            "problemMatcher": "$msCompile"
        }
    ]
}

然而,我无法通过集成调试器命中断点或以其他方式逐步执行单元测试。我想到的最接近的启动配置可以运行测试,但调试器不会命中断点或连接到任何内容。

    {
        "name": "test",
        "type": "coreclr",
        "request": "launch",
        "preLaunchTask": "build",
        "program": "dotnet",
        "args": ["test"],
        "cwd": "${workspaceRoot}/src/tests",
        "stopAtEntry": true,
        "console": "internalConsole"
    }

我可能会错过一些基础知识,但是如何启动或附加VSCode C#调试器到MSTest单元测试呢?


可能是在VisualStudio Code中调试MSTest单元测试的重复问题。 - Arghya C
我无法在dotnet test上使用"problemMatcher": "$msCompile",你能用吗? - Anthony Mastrean
1个回答

15

由于找不到更优雅的解决方案,我最终做成了这样:

创建一个名为launchMsTestAndWaitForDebugger.bat的文件,并添加以下内容:

set VSTEST_HOST_DEBUG=1
dotnet test Path\\To.Your\\Tests.csproj

这将启动dotnet test并等待调试器连接。运行它还将显示进程ID,这将在以后有用。

Starting test execution, please wait...
Host debugging is enabled. Please attach debugger to testhost process to continue.
Process Id: 13292, Name: dotnet

接着我在tasks.json中创建了一个任务来运行这个.bat文件:

{
    "label": "Start MS Test",
    "type": "shell",
    "isBackground": true,
    "command": "${cwd}\\Path\\To.Your\\launchMsTestAndWaitForDebugger.bat",
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared"
    },
    "problemMatcher": []
}

现在我们可以启动 dotnet test 并等待调试器,很棒。确保 launch.json 中有一个条目来附加到一个进程:

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

现在按下ctrl+shift+p并运行Start MS Test任务。查找输出中的进程ID。使用.NET Core Attach定义启动,选择正确的进程并点击播放。完成:

enter image description here


12
在 Code v1.45.1 中,我定义了一个任务来使得其不需要 .bat 文件就能工作(因此跨平台?):{ "label": ".NET Core Test with debugger", "type": "process", "isBackground": true, "command": "dotnet", "args": [ "test" ], "options": { "cwd": "${workspaceFolder}/MyProject.Tests", "env": { "VSTEST_HOST_DEBUG": "1" }, }, "group": "test", "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" }, "problemMatcher": [] }。将其分配到 test 组使其可通过“Tasks: Run test task”命令运行。 - Lance U. Matthews
2
这仍然是最好的建议吗?没有什么“开箱即用”的方法可以让它正常工作吗? - Michael Welch
1
我总是收到这个消息:匹配指定模式的测试文件总数为1。 主机调试已启用。请附加调试器到testhost进程以继续。 进程ID:63395,名称:dotnet - Michael Welch
1
@MichaelWelch,您的测试流程已经启动,现在您需要按照上面的答案所述附加:“使用.NET Core Attach定义启动,选择正确的进程并点击播放。完成!” - Felix K.
在我的 Windows 机器上,我必须使用以下语法:$env:VSTEST_HOST_DEBUG=1 才能使其工作。 - Jason D
你知道如何通过运行和调试面板运行你的 .bat 文件吗? - john k

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接