finally 块仅在调试模式下执行 [英] Finally block is executed only on Debug mode

查看:45
本文介绍了finally 块仅在调试模式下执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题来自此处:SO - c# 测试中的超时

我创建了一个简单的单元测试,如下所示:

I created a simple unit test like this:

[Timeout(1000)][TestMethod]
public void TestMethod1()
{
    try
    {
        System.Threading.Thread.Sleep(2000);
    }
    finally
    {
        Console.WriteLine("Executed");
    }
}

当我运行测试时,finally 块没有被执行.但是当我调试它时,它确实如此.为什么会发生这种情况?

When I run the test, the finally block is not executed. But when I debug it, it does. Why is this happening?

推荐答案

您将测试超时指定为 1000 毫秒,而您的方法休眠 2000 毫秒.这会导致您的测试方法过早地被测试框架强制关闭,因此它不会离开 Sleep 调用并且没有时间到达 finally 块.调试可能会禁用超时属性.

You specify the test timeout at 1000 ms, while your method sleeps for 2000 ms. This results in your test method being prematurely force-closed by the test framework, so it doesn't leave the Sleep call and doesn't have time to reach finally block. Debugging probably disables the timeout attribute.

这篇关于finally 块仅在调试模式下执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆