Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Visual Studio break on exceptions when debugging unit tests?

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?

like image 354
Jeff Stewart Avatar asked Sep 28 '09 17:09

Jeff Stewart


People also ask

How do I enable Break on exception in Visual Studio?

Tell the debugger to break when an exception is thrownIn the Exception Settings window (Debug > Windows > Exception Settings), expand the node for a category of exceptions, such as Common Language Runtime Exceptions. Then select the check box for a specific exception within that category, such as System.


2 Answers

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.

like image 144
JaredPar Avatar answered Oct 04 '22 15:10

JaredPar


For me I am using VS 2010 and 2015, Go to: Tools, Options, Debugging, General: you need to make sure the "Enable Just My Code" and "Enable the exception assistant" should be checked.

like image 21
Haryono Avatar answered Oct 04 '22 13:10

Haryono