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

前端 未结 6 640
粉色の甜心
粉色の甜心 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条回答
  •  -上瘾入骨i
    2021-01-17 11:09

    Generally I try to avoid starting threads in unit tests by injecting a thread provider that, during unit tests, doesn't actually provide a thread, but rather executes syncronously.

    Of course, with that approach you can't test any kind of potential contention, or two real code paths running in parallel, but there is a good argument to be made that such a test isn't really a unit test.

    When you do test two simultaneous threads, then you need a thread provider which catches exceptions at the end of the the thread and reports them as failures.

提交回复
热议问题