9

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?

dkackman
  • 15,179
  • 13
  • 69
  • 123
  • Possible duplicate of [Debugging MSTest Unittests in VisualStudio Code](https://stackoverflow.com/questions/43210794/debugging-mstest-unittests-in-visualstudio-code) – Arghya C Nov 15 '18 at 18:59
  • I can't get `"problemMatcher": "$msCompile"` to work on `dotnet test` for me, does it work for you? – Anthony Mastrean Jul 12 '19 at 19:39

1 Answers1

14

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

granaker
  • 1,318
  • 8
  • 13
  • 12
    +1. In Code v1.45.1 I got this to work without needing the `.bat` file (and, therefore, cross-platform?) by defining a task as follows: `{ "label": ".NET Core Test with debugger", "type": "process", "isBackground": true, "command": "dotnet", "args": [ "test" ], "options": { "cwd": "${workspaceFolder}/MyProject.Tests", "env": { "VSTEST_HOST_DEBUG": "1" }, }, "group": "test", "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" }, "problemMatcher": [] }`. Assigning it to the `test` group makes it runnable by the `Tasks: Run test task` command. – Lance U. Matthews May 26 '20 at 01:25
  • 2
    Is this still the best advice? There's nothing "out of the box" to just make this work? – Michael Welch Jul 25 '20 at 16:33
  • 1
    I always get this message: A total of 1 test files matched the specified pattern. Host debugging is enabled. Please attach debugger to testhost process to continue. Process Id: 63395, Name: dotnet – Michael Welch Jul 25 '20 at 16:36
  • 1
    @MichaelWelch your test process has been started, now you need to attach as written in the answer above: "Launch using the .NET Core Attach definition, pick the right process and hit play. Voila" – Felix K. Sep 28 '20 at 15:37
  • On my Windows machine I had to use the syntax: $env:VSTEST_HOST_DEBUG=1 to make this work. – Jason D Feb 23 '22 at 18:48
  • do you know a way to run your .bat file through the run and debug panel? – john k Mar 08 '23 at 04:46