Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one debug an MSTest in VSCode?

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?

like image 369
dkackman Avatar asked Nov 02 '17 01:11

dkackman


1 Answers

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:

enter image description here

like image 84
granaker Avatar answered Oct 27 '22 02:10

granaker