How to: Debug while Running a Smart Device Unit Test

Automatic debugging is not supported for smart device unit tests because the test code runs on a device or emulator instead of on a desktop computer. Selecting Start Debugging in the Debug menu, Debug Selection in the Test View window, or Debug Checked tests in the Test List Editor only runs a test and does not debug a test. This topic describes how to debug by manually attaching the debugger to a running smart device unit test.

In the following procedures, you create a smart device unit test, prepare the device for debugging, add breakpoints to the test, and debug the test.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

To debug while running a smart device unit test

  1. Create a smart device application that you want to test and a smart device unit test to test it.

    For more information, see Walkthrough: Creating and Running a Smart Device Unit Test.

  2. To enable managed debugging on the device, use the Visual Studio Remote Registry Editor to create the following items in the device's registry:

    1. A new Key under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETCompactFramework named Managed Debugger.

    2. A new DWORD value under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETCompactFramework\Managed Debugger with the name AttachEnabled and value 1.

  3. Set breakpoints by adding the statement System.Diagnostics.Debugger.Break() to the smart device unit test.

    Note

    Breakpoints in your code will be hit only if they follow this System.Diagnostics.Debugger.Break() statement.

  4. Start your test.

    To do this, in the Test View window, right-click the test you want to run and then click Run Selection.

  5. Wait for the test host to hit a breakpoint that you added to your unit test. When this occurs, your device will display the message: "A user break has occurred in SmartDeviceTestHost.exe."

  6. Attach to the smart device test host process:

    1. On the Debug menu, click Attach to Process.

    2. Change the Transport to Smart Device.

    3. Change the Qualifier to the device that is running the test.

    4. Click SmartDeviceTestHost.exe in the list of Available Processes.

    5. Click Attach.

    Visual Studio will attach to the process with execution paused at the System.Diagnostics.Debugger.Break() statement. You can click Continue on the Debug menu or press F5 to enable execution to continue to your next breakpoint.

    Note

    Remove the System.Diagnostics.Debugger.Break() statement and Managed Debugger registry key when you have finished debugging. Otherwise, the breakpoint dialog box will appear every time that you run the test.

See Also

Tasks

How to: Attach to Managed Device Processes

How to: Create a Smart Device Unit Test

Other Resources

Working with Smart Device Unit Tests