What will NUnit do internally when the validation timeout fails?

exactly what does NUnit do when it encounters a timeout? I used to think he 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 reports only a timeout violation, but not how the test was interrupted by it. is there anyone who knows more about how this will work? and why the above test did not raise the TimeoutException that I was expecting? (although this is a type of .NET exception, I decided that NUnit used this exception for timeout violations).

PS: this testing method also fails:

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

and this testing method succeeds ("no one expects the Spanish Inquisition!"):

 [Test, ExpectedException(typeof(ThreadAbortException))] public static void ThrowingExpectedThreadAbortException() { Thread.CurrentThread.Abort(); } 
+4
c # nunit timeoutexception
source share
2 answers

If the test method in NUnit indicates a timeout, it will be run in a separate thread from the rest of the tests. If the test exceeds the timeout, the created thread will be roughly interrupted using Thread.Abort .

The interruption part is not specified in the documentation, but is obvious when digging into the NUnit code base. See TestThread.RunTest more details.

EDIT *

The reason why the test fails, and when it expires, even if you catch an exception, is that the test is marked as a failure before it interrupts the flow. Therefore, regardless of whether it was expected, it is pointless, as it is already marked as unsuccessful.

+5
source share

NUnit never causes an exception to magically appear in your code. The NUnit tester launches your code from a thread and interrupts this thread if it takes longer than the specified timeout.

When the thread executing your code is interrupted, a ThreadAbortException . Since NUnit knows that this caused this exception, adding an ExpectedException(typeof(ThreadAbortException)) will not pass the test.

+1
source share

All Articles