what will NUnit do internally when a test timeout is violated?

风格不统一 提交于 2019-12-22 09:09:16

问题


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);
}

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: 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();
}

回答1:


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.

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.

EDIT*

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.




回答2:


NUnit never causes an exception to magically appear from inside your code. The NUnit test runner is running your code from with a thread and aborts that thread if it takes longer than the specified timeout.

When the thread running your code is aborted a ThreadAbortException is generated. Since NUnit knows it caused this exception itself, adding an ExpectedException(typeof(ThreadAbortException)) isn't going to cause the test to pass.



来源:https://stackoverflow.com/questions/9448435/what-will-nunit-do-internally-when-a-test-timeout-is-violated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!