views:

477

answers:

5

When I attempt to debug a unit test that fails because of an unhandled exception in my code, I expect Visual Studio to break on the unhandled exception so I can inspect the code and isolate the problem. Instead, the IDE instantly exits debug mode and the test is listed as "Failed", leaving me to consult the test result's stack trace to find the problem.

I've confirmed that the IDE is configured to break when any user-unhandled CLR exception is thrown. I can only get the expected behavior if I configure the IDE to break on all thrown exceptions. This, of course, makes normal debugging a PITA.

Am I out of luck?

+1  A: 

Install Testdriven.NET, and use "Test With -> Debugger".

mxmissile
+1  A: 

The unit testing framework handles the exception so visual studio thinks that the exception is handled.

tster
+2  A: 

The reason why is that your exceptions are not unhandled. The unit testing framework is handling the exceptions under the hood and converting them to failures.

What you need to do instead is to break on thrown exceptions. If you combine this with enabling "Just My Code" (on by default) you should get the behavior you are expecting. Visual Studio will only break when exceptions thrown by your code occur.

JaredPar
This is an acceptable workaround for now, but later on when my code matures and legit exceptions are thrown more often it will become a PITA. Groan.
Jeff Stewart
I get why, buyt I don't understand the workaround; in thicko terms, what do I click? For me, even "Debug Tests" doesn't hit breakpoints.
Luke Puplett
@Luke Puplett: Apparently, "Debug" menu -> "Exceptions..." -> Check "thrown" for the relevant exceptions.
FunctorSalad
+1  A: 
The Matt
That doesn't work unless I configure the debugger to break on all *thrown* (rather than user-unhandled) exceptions, which is what I wanted to avoid.
Jeff Stewart
+1  A: 

By default, even if you do a debug build, MSTest does't give you debugging (with break points, etc), unless you actually tell it to explicitly "Debug Unit Tests".

It should break on unhandled exceptions in Unit testing, provided you are in a Debug configuration, and you start the Unit Testing using "Debug" - ie: Using Ctrl+R, Ctrl+A instead of Ctrl+R, A to run all tests. In the testing window, there is a "Run" and a "Debug" menu with the options.

Reed Copsey
I was explicitly trying "Debug Unit Tests" using CTRL+R,CTRL+A or CTRL+R,CTRL+T. Again, this approach only works if the debugger is configured to break on all *thrown* (rather than user-unhandled) exceptions, which is what I wanted to avoid.
Jeff Stewart