I had a request from a coworker today who wanted to be able to step through a suite of unit tests to see the flow of the tests and examine the variables along the way. We are using the NUnit GUI and not any of the other types of addins for Visual Studio .NET (2002 or 2003), but the following instructions should apply with any other tool as well:
- To start the debugging, select a line of code in your unit test method and place a breakpoint on it.
- Launch the NUnit GUI (if not already running)
- In VS.NET, go to your Debug menu and select Processes.
- Find the running process for NUnit (“nunit-gui.exe”) and click “Attach”
- Select the “Common Language Runtime” as the program type you wish to debug and hit “OK”.
- Switch back to NUnit, select the test fixture where you placed your breakpoint, and click “Run” to start the test.
- VS.NET should automatically break on the line of code where you attached your breakpoint, and then you can step through your code like you would in a normal debugging session.
Ideally, if you follow the spirit of unit testing you will not have the need to do this very often. However, if you are trying to see the flow of an aggregate of tests, this can be a very useful tool to see the flow without resorting to outputting statements to your console window or trace listeners.
Learning to use the debugger to attach to running processes is a very powerful feature. Additional uses include debugging user controls during design time, attaching to code running on remote machines, and debugging serviced components. If there is any interest from the community, I can put together a more complete guide for this useful feature of VS.NET.
Technorati Tags: nunit, nunit debug