如何在MSTest中处理currentDomain.UnhandledException

我尝试基于答案实现解决方案如何处理unit testing时在其他线程中引发的exception? ,但我仍然不明白在处理程序中要做什么。 我们假设我有一个测试:

[TestMethod] void Test() { new Thread(() => { throw new Exception(); }).Start(); } 

我有和所有测试的全局初始化:

 [AssemblyInitialize] public static void AssemblyInitialize(TestContext context) { AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += currentDomain_UnhandledException; } static void currentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Exception ex = e.ExceptionObject as Exception; if (ex != null) Trace.WriteLine(ex); Assert.Fail("Unhandled Exception in thread."); } 

问题是Assert.Fail实际上抛出exception,它再次被currentDomain_UnhandledException捕获并导致MSTest崩溃(stackoverflow?)。 我不想捕获Assert.Fail,但我想让测试失败。 怎么解决?

我知道我可以捕获exception并在测试的主线程上调用它,但我需要全局解决方案进行数千次测试。 我不想让每一个测试都复杂化。