How does one debug an MSTest in VSCode?

喜夏-厌秋 提交于 2019-12-12 20:22:57

问题


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>.

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

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


回答1:


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

Create a launchMsTestAndWaitForDebugger.bat file with this:

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

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

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": []
}

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

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:



来源:https://stackoverflow.com/questions/47066356/how-does-one-debug-an-mstest-in-vscode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!