How to handle exceptions raised in other threads when unit testing?

前端 未结 6 688
粉色の甜心
粉色の甜心 2021-01-17 10:43

When testing with Visual Studio Team Test unhandled exceptions in tests are caught and reported in the results. So I was kind of surprised to see the test hosting process (V

6条回答
  •  甜味超标
    2021-01-17 11:15

    A couple of ideas:

    • Use BackgroundWorker to do the unit test work. BackgroundWorker will automatically catch unhandled exceptions and report them in Error property of RunWorkerCompletedEventArgs. However you will need a way to block the unit test thread until BackgroundWorker completes the work.
    • This one is a not good option by itself and may not even be suitable for your testing goals. Nonetheless I wanted to mention. You can go back to how unhandled exceptions from other threads were treated in .NET 1.0 and 1.1 by using legacyUnhandledExceptionPolicy. Prior to .NET 2.0, unhandled exceptions from threads were quietly ignored. However, in .NET 2.0, they actually cause the application to terminate. legacyUnhandledExceptionPolicy setting allows you to have pre .NET 2.0 behavior.

提交回复
热议问题