Search code examples
c#nunittimeoutexception

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


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

Solution

  • 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.