在 Visual Studio 代码中调试 MSTest 单元测试

Debugging MSTest Unittests in Visual Studio Code

我正在尝试使用 Visual Studio 代码来调试 MSTest 单元测试项目。但是测试只是 运行 并且从未达到断点。

这是我的 launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Test (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "C:\Program Files\dotnet\dotnet.exe",
            "args": ["test"],
            "cwd": "${workspaceRoot}",
            "console": "internalConsole",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart"
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}

如何调试单元测试 (MSTest)? XUnit 也存在同样的问题。

尝试 https://github.com/Microsoft/vstest-docs/blob/master/docs/diagnose.md#debug-test-platform-components(假设您使用的是 dotnet-cli 工具 1.0.0)

> set VSTEST_HOST_DEBUG=1
> dotnet test
# Process will wait for attach
# Set breakpoint in vscode
# Use the NETCore attach config from vscode and pick the dotnet process

如果您使用的是最新版本的 VS Code(我使用的是 v1.29.0),调试单元测试是内置功能。

您需要先构建解决方案 dotnet build 才能显示测试 运行 和调试选项。

尝试重新加载 VS Code,对我有用。 按 Ctrl+Shift+P 打开命令面板,然后:

Reload Window

在构建之前,记得将其包含在您的 .csproj 文件中

<GenerateProgramFile>false</GenerateProgramFile>

否则它不知道要做什么 运行...

Program.cs(160,21): error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point. [/Users/.../Fraction.csproj]