Search code examples
unit-testingvisual-studio-2022mixed-mode

Using mixed-mode debugging on a managed unit test in an SDK-style project in Visual Studio 2022


I have a C# unit test project that I would like to debug with mixed-mode debugging when I select Debug on a test from the Test Explorer.

In the properties dialog for non-SDK-style projects I can choose "Enable native code debugging" under the Debug tab as shown in this answer: https://stackoverflow.com/a/29245531/1555496. However the properties dialog for SDK-style project have been through a complete overhaul and the project-wide debug settings have been replaced with launch profiles.

I am not however launching with a launch profile when I choose Debug from the Test Explorer, so how do I enable mixed mode debugging now?


Solution

  • It turns out that Visual Studio uses what seems to be the first one of the launch profile for the test project when selecting Debug through Test Explorer.

    1. Go to the properties page for your unit test project (right-click on project in Solution Explorer, then click "Properties")
    2. Go to the Debug tab (probably second-to-last in the list at the left-hand side)
    3. Under the Debug section click "Open debug launch profiles UI"
    4. Now select the profile that is just the name of the project in the list at the left-hand side (should be the only one if you haven't edited the launch profiles for the project before)
    5. Enable the checkbox "Enable debugging for managed and native code together, also known as mixed-mode debugging." under "Enable native code debugging" (you may have to scroll down to see it) Selecting Open debug launch profiles UI Enabling native code debugging in Launch Profiles UI

    You can also edit/create the Properties/launchSettings.json file yourself

    {
        "profiles": {
            "FooBar.MyTestProject": {
                "commandName": "Project",
                "nativeDebugging": true
            }
        }
    }
    

    It seems that the launch profile must be the first one listed and of type ("commandName") "Project" in order for it to be used. The name of the profile does not seem to matter however.