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

2

4

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();
}
Plourde answered 25/2, 2012 at 21:55 Comment(0)
A
5

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.

Angieangil answered 25/2, 2012 at 22:8 Comment(0)
R
1

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.

Reticular answered 25/2, 2012 at 22:6 Comment(3)
Note: A thread abort does actually cause an exception to appear in your code an abort is implemented via a ThreadAbortExceptionAngieangil
Agreed, I meant that there was no way it would make a TimeoutException or any other exception appear, apart from ThreadAbortException as you point out.Reticular
in that case, why does that test method also fail if I set ExpectedException to ThreadAbortException?Plourde

© 2022 - 2024 — McMap. All rights reserved.