Share via


How to: Debug while Running a Test in an ASP.NET Solution

You can debug both unit tests and Web tests that you are using to test ASP.NET Web projects. You can also debug your ASP.NET production code while a test is running.

However, when you are testing ASP.NET code, the test code does not run in the typical way, under the VSTestHost process. Instead, it runs in either the IIS process, or if your Web site is file-based, in an ASP.NET Development Server process. Because the test runs in other than the typical test process, choosing the Debug Selection option in the Test View window or the Debug Checked tests option in the Test List Editor does not debug while the test is running.

Similarly, ASP.NET production code cannot be stepped into unless debugging is enabled and the debugger is attached to the ASP.NET Development Server or the IIS process.

In each of the following procedures, you create an ASP.NET Web site project by using the New Web Site dialog box. In this dialog box, you use the Location list box to choose a location for the Web site. This choice determines the process to which you will later attach the debugger, and the steps that you follow to debug while running a test in an ASP.NET solution. The two options are IIS and ASP.NET Development Server.

  • Debugging While Running on IIS

  • Debugging While Running on ASP.NET Development Server

Debugging While Running on IIS

In the following procedure, you will attach the debugger to a process. The name of the process depends on the operating system you are using, because the operating system determines the version of IIS that is installed. The following table shows what process is used on each operating system. For an operating system in the first column, use the name of the process specified in the third column in the Available Processes section of the Attach to Process dialog box.

Operating System

Host process

ASP.NET Process name

Windows Server 2003

IIS 6.0

w3wp.exe

Windows XP

IIS 5.1

aspnet_wp.exe

Windows 2000

IIS 5.0

aspnet_wp.exe

To debug while running an ASP.NET test on IIS

  1. Create an ASP.NET Web site project that you want to test, and an ASP.NET unit test to test it.

    When you create this Web site project, in the New Web Site dialog box, select HTTP or FTP as the Location. This guarantees that the host process will be IIS.

    For more information, see How to: Create an ASP.NET Unit Test.

  2. Enable debugging in the ASP.NET Web project. To do this, edit the project's Web configuration file. If your project already has a Web.config file, go to step 4. If not, add one now: Right-click the Web site project in your solution, and then click Add New Item.

    The Add New Item dialog box is displayed.

  3. In the Add New Item dialog box, click Web Configuration File and then click Add.

    A Web.config file is added to your Web site project. For more information about how to use Web.config files, see How to: Enable Debugging for ASP.NET Applications.

  4. In the Web.config file, change the line that reads <compilation debug="false"/> to <compilation debug="true"/>.

  5. (Optional) Set breakpoints in the ASP.NET unit test and in the production code for your Web site.

  6. Attach the debugger to the IIS process. To do this, click Debug and then click Attach to Process.

    The Attach to Process dialog box is displayed.

  7. Under Available Processes, locate the name of the process to which you want to attach the debugger.

    If the process you need is not displayed, you can display more processes that are running. To do this, check Show processes from all users and Show processes in all sessions.

  8. Under Available Processes, click the name of the process and then click Attach.

    If the process to which you have to attach is not running, click Cancel and then start the process. To do this, in Solution Explorer, right-click the Web site project and then click View in Browser. Next, click Debug and then click Attach to Process one more time; the process has started running and is displayed under Available Processes. Click the process name and then click Attach.

  9. Start your test.

    To do this, in the Test View window, right-click the test your want to debug and select Debug Selection.

    Note

    When running tests, you can choose Run Selection instead of Debug Selection. The difference is that if you choose Debug Selection, the debugger automatically detaches after the test finishes, but if you choose Run Selection, the debugger stays attached. You may find Run Selection more useful, especially if you plan to rerun the test several times while debugging, and you do not want to have to reattach the process every time. If you have chosen Run Selection, to detach the process when you have finished debugging, choose Stop Debugging from the Debug menu or press Shift+F5.

Debugging While Running on ASP.NET Development Server

To debug while running a test on ASP.NET Development Server

  1. Create an ASP.NET Web site project that you want to test, and an ASP.NET unit test to test it.

    When you create this Web site project, in the New Web Site dialog box, select File System as the Location. This guarantees that the host process will be ASP.NET Development Server.

    For more information, see How to: Create an ASP.NET Unit Test.

  2. Enable debugging in the ASP.NET Web project. To do this, edit the project's Web configuration file. If your project already has a Web.config file, go to step 4. If not, add one now: Right-click the Web site project in your solution, and then click Add New Item.

    The Add New Item dialog box is displayed.

  3. In the Add New Item dialog box, click Web Configuration File and then click Add.

    A Web.config file is added to your Web site project. For more information about how to use Web.config files, see How to: Enable Debugging for ASP.NET Applications.

  4. In the Web.config file, change the line that reads <compilation debug="false"/> to <compilation debug="true"/>.

  5. Add the statement System.Diagnostics.Debugger.Break() to the beginning of your ASP.NET unit test.

    Note

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

    Note

    In C++, this statement is System::Diagnostics::Debugger::Break(). For information about how to use unit tests in Visual C++, see Unit Tests and C++.

  6. Set breakpoints in the ASP.NET unit test and in the production code for your Web site.

  7. Start your test.

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

  8. As soon as the test starts to run, a dialog box appears. This dialog box indicates that the program has encountered a breakpoint.

  9. Click Debug, choose the instance of Visual Studio that has your solution loaded, and then click Yes.

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

    Note

    Be sure remove the System.Diagnostics.Debugger.Break() statement when you have finished debugging. Otherwise, the breakpoint dialog box will appear every time that you run the test.

See Also

Tasks

How to: Debug while a Test is Running

Other Resources

ASP.NET Unit Tests

How to: Create an ASP.NET Unit Test

How to: Enable Debugging for ASP.NET Applications