如何在VSCode中调试MSTest? [英] How does one debug an MSTest in VSCode?

查看:326
本文介绍了如何在VSCode中调试MSTest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VSCode 1.17.2版本中(安装了C#扩展),我已通过dotnet new mstest将MSTest项目添加到解决方案文件夹,并添加了对使用dotnet add <project_path>测试的程序集的引用.

In version 1.17.2 of VSCode (with the C# extenion installed) I have added an MSTest project to a solution folder via dotnet new mstest and added a reference to the the assembly being tested with dotnet add <project_path>.

鉴于以下两个VSCode任务,我可以成功构建并运行测试;也就是说,一切都建立了,单元测试运行并通过了.

Given the two VSCode tasks below I can build and run the tests successfully; i.e. everything builds, the unit tests run and pass.

{
    "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"
        }
    ]
}

但是,我无法达到断点,也无法通过集成调试器逐步完成单元测试.我提供的最接近的启动配置将运行测试,但调试器不会达到断点或附加任何东西.

I cannot however hit breakpoints or otherwise step through the unit test with the integrated debugger. The closest launch configuration I've come up with will run the tests but the debugger doesn't hit breakpoints or attach to anything.

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

我可能缺少一些基本知识,但是如何将vscode c#调试器启动或附加到MSTest单元测试中呢?

I may be missing something fundamental but how does one launch or attach the vscode c# debugger to an MSTest unit test?

推荐答案

在缺乏更优雅的解决方案的情况下,我最终这样做:

In lack of a more elegant solution, I ended up doing this:

使用以下命令创建launchMsTestAndWaitForDebugger.bat文件:

Create a launchMsTestAndWaitForDebugger.bat file with this:

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

这将启动dotnet test,并等待附加调试器. 运行它还将显示进程ID,稍后将有帮助.

This will launch dotnet test and wait for a debugger to be attached. Running it will also display the process id, which will help later..

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文件:

Next I created a task in tasks.json to run this .bat-file:

{
    "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中有一个条目可以附加到进程:

So now we can launch dotnet test and wait for a debugger, great. Make sure you have an entry in launch.json to attach to a process:

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

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

Now ctrl+shift+p and run the Start MS Test task. Look for the processid in the output. Launch using the .NET Core Attach definition, pick the right process and hit play. Voila:

这篇关于如何在VSCode中调试MSTest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆