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.
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
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.
To enable managed debugging on the device, use the Visual Studio Remote Registry Editor to create the following items in the device's registry:
A new Key under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETCompactFramework named Managed Debugger.
A new DWORD value under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETCompactFramework\Managed Debugger with the name AttachEnabled and value 1.
Set breakpoints by adding the statement System.Diagnostics.Debugger.Break() to the smart device unit test.
Breakpoints in your code will be hit only if they follow this System.Diagnostics.Debugger.Break() statement.
Start your test.
To do this, in the Test View window, right-click the test you want to run and then click Run Selection.
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."
Attach to the smart device test host process:
On the Debug menu, click Attach to Process.
Change the Transport to Smart Device.
Change the Qualifier to the device that is running the test.
Click SmartDeviceTestHost.exe in the list of Available Processes.
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.
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.