dka*_*man 7 .net unit-testing mstest visual-studio-code
在 VSCode 的 1.17.2 版(安装了 C# 扩展)中,我通过将一个 MSTest 项目添加到解决方案文件夹中,dotnet new 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"
}
]
}
Run Code Online (Sandbox Code Playgroud)
但是,我无法使用集成调试器点击断点或以其他方式单步执行单元测试。我提出的最接近的启动配置将运行测试,但调试器不会命中断点或附加到任何内容。
{
"name": "test",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "dotnet",
"args": ["test"],
"cwd": "${workspaceRoot}/src/tests",
"stopAtEntry": true,
"console": "internalConsole"
}
Run Code Online (Sandbox Code Playgroud)
我可能缺少一些基本的东西,但是如何启动或将 vscode c# 调试器附加到 MSTest 单元测试?
小智 10
由于缺乏更优雅的解决方案,我最终这样做了:
launchMsTestAndWaitForDebugger.bat用这个创建一个文件:
set VSTEST_HOST_DEBUG=1
dotnet test Path\\To.Your\\Tests.csproj
Run Code Online (Sandbox Code Playgroud)
这将启动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
Run Code Online (Sandbox Code Playgroud)
接下来我在 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": []
}
Run Code Online (Sandbox Code Playgroud)
所以现在我们可以启动 dotnet 测试并等待调试器,太好了。确保您在 launch.json 中有一个条目可以附加到进程:
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
Run Code Online (Sandbox Code Playgroud)
现在ctrl+shift+p运行Start MS Test任务。在输出中查找 processid。使用.NET Core Attach定义启动,选择正确的过程并点击播放。瞧: