gpt4 book ai didi

.net - 如何在 VSCode 中调试 MSTest?

转载 作者:行者123 更新时间:2023-12-03 15:57:50 25 4
gpt4 key购买 nike

在 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 单元测试?

最佳答案

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

创建一个 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 启动定义,选择正确的流程并点击播放。瞧:

enter image description here

关于.net - 如何在 VSCode 中调试 MSTest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47066356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com