Search code examples
visual-studiocsprojvstestvstest.console.exevstesthost

How do I debug a custom test adapter in Visual Studio


I am working on a custom test adapter for Visual Studio 2017. How can I configure Visual Studio to debug the test adapter without having to use a hack such as adding Debugger.Launch() in my adapter code?


Solution

  • Microsoft Child Process Debugging Power Tool

    Install the Microsoft Child Process Debugging Power Tool which was created by an employee at Microsoft. This allows you to configure the Visual Studio debugger to attach to child processes (which is how vstest.console.exe executes tests)

    Once installed, open your solution and enable child process debugging: 1) Go to the child process debug settings at the following Visual Studio Menu location: Debug -> Other Debug Targets -> Child Process Debugging Settings... 2) Enable child process debugging: true and Save 3) Optionally persist the settings using the dropdown so that this setting can be checked into source control

    If you choose to persist the settings, your settings file might look something like this:

    <?xml version="1.0" encoding="utf-8"?>
    <!-- EngineFilter Guid was found here: https://blogs.msdn.microsoft.com/martintracy/2006/05/16/debug-engine-guids/ -->
    <ChildProcessDebuggingSettings IsEnabled="true" xmlns="http://schemas.microsoft.com/vstudio/ChildProcessDebuggingSettings/2014">
        <DefaultRule Attach="false" />
        <Rule IsEnabled="true" ProcessName="TE.ProcessHost.Managed.exe" EngineFilter="{92ef0900-2251-11d2-b72e-0000f87572ef}" />
        <Rule IsEnabled="true" ProcessName="vstest.discoveryengine.exe" EngineFilter="{92ef0900-2251-11d2-b72e-0000f87572ef}" />
        <Rule IsEnabled="true" ProcessName="vstest.discoveryengine.x86.exe" EngineFilter="{92ef0900-2251-11d2-b72e-0000f87572ef}" />
        <Rule IsEnabled="true" ProcessName="vstest.executionengine.exe" EngineFilter="{92ef0900-2251-11d2-b72e-0000f87572ef}" />
        <Rule IsEnabled="true" ProcessName="vstest.executionengine.x86.exe" EngineFilter="{92ef0900-2251-11d2-b72e-0000f87572ef}" />
    </ChildProcessDebuggingSettings>
    

    Once this has been setup, you then just need to make sure your project is setup to debug with vstest.console.exe. The key point here is making sure that you enable native/unmanaged debugging, otherwise the child process debugging tool wont work.


    New csproj System

    Edit or create a launchSettings.json file to look similar to this:

    {
        "profiles": {
            "DebugTestAdapter": {
                "commandName": "Executable",
                "executablePath": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\Common7\\IDE\\Extensions\\TestPlatform\\vstest.console.exe",
                "commandLineArgs": "Tests.dll --ListTests --TestAdapterPath:.",
                "workingDirectory": "C:\\Projects\\TestAdapter\\Tests\\bin\\Debug\\net46"
            }
        }
    }
    

    Modify your csproj file to contain the following property, which enabled native debugging:

    <PropertyGroup>
        <EnableUnmanagedDebugging>true</EnableUnmanagedDebugging>
    </PropertyGroup>
    

    Old csproj System

    In the debug properties page of your project, set the following settings:

    Start external program:

    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\TestPlatform\vstest.console.exe
    

    Command line arguments:

    Tests.dll --ListTests --TestAdapterPath:.
    

    Working directory:

    C:\Projects\TestAdapter\Tests\bin\Debug
    

    Enable native code debugging: Set this value to true