5

I want to write a unit test for detecting a thrown exception. Using ExpectedExceptionAttribute, I run the test from inside MSVS and when the TestMethod hits the exception, the debugger breaks to the exception but the TestMethod correctly skips over and reports Passed.

Is there a flag to tell VS not to break during unit testing?

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
Jake
  • 11,273
  • 21
  • 90
  • 147

3 Answers3

12

Do not run the tests in a debug session. You have the option to 'Run Selection' or 'Debug selection', choose the first and you should be fine.

ChrisBint
  • 12,773
  • 6
  • 40
  • 62
  • @Jake Use ctrl + F5 in this scenario, which is the same as running the selection (non debug) – ChrisBint Aug 27 '12 at 03:39
  • Yes, I realised my CTRL was not working! So I deleted my previous comment. After reading your answer, I found the "Test" menu so I thought I had to run from there. Thanks. – Jake Aug 27 '12 at 03:43
4

If you press CTRL + ALT + E you can configure whether the debugger will break when different types of exceptions are thrown and/or not handled.

I have found using these settings to be very handy at times.

lockstock
  • 2,359
  • 3
  • 23
  • 39
  • There are lots of options (checkboxes) on that dialog (Debug:Exceptions). I have found that I only need to uncheck the Common Language Runtime Exceptions (line) Thrown (column) box. I agree that another option is to run without the debugger, but sometimes you do want to run with the debugger for some reason. So, this is a good option when you need to use the debugger and don't want to break on first chance exceptions. – steve Oct 09 '14 at 17:09
1

As an alternative to the MS Unit specific [ExpectedException], if you need to debug your unit tests, you can explicitly try/catch the exception and Assert the type of exception, such as done here: How do I use Assert to verify that an exception has been thrown?

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285