Csharp/C#教程:如何在MSTest中处理currentDomain.UnhandledException分享


如何在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并在测试的主线程上调用它,但我需要全局解决方案进行数千次测试。 我不想让每一个测试都复杂化。

上述就是C#学习教程:如何在MSTest中处理currentDomain.UnhandledException分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/983741.html

(0)
上一篇 2021年12月19日
下一篇 2021年12月19日

精彩推荐