当违反测试超时时,NUnit 将在内部做什么? [英] what will NUnit do internally when a test timeout is violated?

查看:83
本文介绍了当违反测试超时时,NUnit 将在内部做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NUnit 在遇到超时时究竟做了什么?我曾经认为它会通过抛出 TimeoutException 来中止测试,但这个测试证明并非如此:

exactly what does NUnit do when it encounters a timeout? I used to think it would abort the test by throwing a TimeoutException, but this test proves otherwise:

[Test, Timeout(100), ExpectedException(typeof(TimeoutException))]
public static void RaisingExpectedTimeoutException()
{
    Thread.Sleep(500);
}

不幸的是,nunit 控制台只报告违反超时的情况,而不报告测试是如何被它中止的.有没有人更了解这将如何工作?为什么上述测试没有引发我预期的 TimeoutException?(尽管它是 .NET 异常类型,但我认为 NUnit 使用该异常来违反超时).

unfortunately the nunit console only reports a violation of the timeout, but not how the test was aborted by it. is there anyone out there who knows more about how this would work? and why the above test did not raise the TimeoutException that I expected? (even though it is a .NET exception type, I figured NUnit used that Exception for timeout violations).

PS:这个测试方法也失败了:

PS: this test method also fails:

[Test, Timeout(100), ExpectedException(typeof(ThreadAbortException))]
public static void RaisingExpectedThreadAbortException()
{
    Thread.Sleep(500);
}

并且这个测试方法成功了(没有人期望西班牙宗教裁判所!"):

and this test method succeeds ("nobody expects the spanish inquisition!"):

[Test, ExpectedException(typeof(ThreadAbortException))]
public static void ThrowingExpectedThreadAbortException()
{
    Thread.CurrentThread.Abort();
}

推荐答案

如果 NUnit 中的测试方法指定了超时,那么它将在与其余测试不同的线程上运行.如果测试超过它的超时时间,创建的线程将通过 Thread.Abort 粗鲁地中止.

If a test method in NUnit specifies a timeout then it will be run on a separate thread from the rest of the tests. If the test exceeds it's timeout the created thread will be rudely aborted via Thread.Abort.

有关中止的部分在文档中没有明确说明,但在深入研究 NUnit 代码库时很明显.有关详细信息,请参阅 TestThread.RunTest.

The part about the abort is not explicit in the documentation but is evident upon digging into the NUnit code base. See TestThread.RunTest for the details.

编辑*

测试在超时时失败的原因,即使您捕获了异常,也是因为测试在中止线程之前被标记为失败.因此,是否预期是没有意义的,因为它已经被标记为失败.

The reason the test fails when it times out, even though you catch the exception, is because the test is marked as failed before it aborts the thread. Hence whether or not it's expected is meaningless because it's already marked as failed.

这篇关于当违反测试超时时,NUnit 将在内部做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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