【问题标题】:How does one debug an MSTest in VSCode?如何在 VSCode 中调试 MSTest?
【发布时间】:2018-04-14 10:47:03
【问题描述】:

在 VSCode 版本 1.17.2(安装了 C# 扩展)中,我通过 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 单元测试?

【问题讨论】:

标签: .net unit-testing visual-studio-code mstest


【解决方案1】:

由于缺乏更优雅的解决方案,我最终这样做了:

用这个创建一个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 测试并等待调试器了,太好了。确保您在 launch.json 中有一个条目可以附加到进程:

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

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

【讨论】:

  • +1。在代码 v1.45.1 中,我通过如下定义任务:{ "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": [] },无需.bat 文件(因此,跨平台?)即可工作。将其分配给test 组使其可以通过Tasks: Run test task 命令运行。
  • 这仍然是最好的建议吗?没有什么“开箱即用”可以让这项工作发挥作用?
  • 我总是收到此消息:共有 1 个测试文件与指定的模式匹配。主机调试已启用。请将调试器附加到 testhost 进程以继续。进程 ID:63395,名称:dotnet
  • @MichaelWelch 您的测试过程已经开始,现在您需要按照上面的答案进行附加:“使用 .NET Core 附加定义启动,选择正确的过程并点击播放。瞧”
猜你喜欢
  • 2020-07-25
  • 2020-03-09
  • 2023-03-24
  • 2021-08-03
  • 1970-01-01
  • 2023-01-09
  • 2021-08-08
  • 2019-04-20
  • 1970-01-01
相关资源
最近更新 更多