Follow @RoyOsherove on Twitter

Debugging Unit tests using the VS.net debugger

[Update:Bah! how silly am I?

Turns out you can do the same thing I outline here only much much easier using the amazing NUnit-Addin.  It has a "Test with Debugger" command that allows you to do just what I explain here without needing to invoke the Nunit gui program from the debugging options. How very cool. Guess you learn something new every day...]

 

Here's a very easy way to debug your unit tests line by line using the vs.net debugger (you shouldn't need to do this too often if you're doing Test Driven Development correctly though):

  • Set the testing project as the startup project in the solution(so you can start debug with F5)
  • Open the project properties of the project with all the tests
  • In the Configuration Properties->Debugging , set the "start action" to "start external program" and select the Nunit-gui.exe or the Nunit-console.exe
  • If you selected the console program, put the name of your assembly in the parameters to send to this program

That's it. Now, to start debugging:

  • Place a breakpoint anywhere in the code that will be tested (either a test or a code that is used by the test)
  • Hit F5.
  • If you chose to debug using the Nunit gui application, just hit "run" on the tests you want and let the debugger stop at the breakpoint you want.If you are debugging by starting the console application you will get to that breakpoint automatically eventually (all the tests in the assembly will be run).

 

Find out more in this article

Weekend reading: Acm Turing Award Lectures : The First Twenty Years, 1966 to 1985

Common rants and misconceptions