This is a mirror of official site: http://jasper-net.blogspot.com/

NUnit Timed Tests

| Monday, June 6, 2011
Sometimes, when performing automated testing, it is necessary to ensure that a test is completed within a given period of time. NUnit helps this type of testing with attributes that fail tests that run for longer than a specified duration.

Timeout Attribute

If you are creating NUnit-based tests that should run within a given duration, you can enforce this using the Timeout attribute. This attribute allows you to specify a number of milliseconds within which the test must complete. If the time limit is exceeded, the test is aborted and flagged as a failure.

This type of timeout is useful when testing code that includes looping or recursion. If there is a logical error and the looping continues indefinitely, the NUnit test runner will abort the test and continue with the next. The attribute can also be useful if you are performing integration testing with a potentially slow dependency.

The sample fixture below shows two tests, each with a two second timeout applied.

[TestFixture]
public class TimeoutTests
{
    [Test, Timeout(2000)]
    public void PassingTest()
    {
        Thread.Sleep(1000);
    }

    [Test, Timeout(2000)]
    public void FailingTest()
    {
        Thread.Sleep(3000);
    }
}

The first test, "PassingTest", includes code that pauses the thread for one second. This is within the timeout value of two seconds and there are no other failures so the test passes. The second test includes a three second pause, which means that two second timeout is exceeded and the test is stopped. The result is a failure with the following message:

Test exceeded Timeout value of 2000ms

The Timeout attribute may also be applied to a test fixture, as shown below. In this situation the timeout duration is individually applied to each test within the fixture.

Read more: BlackWasp

Posted via email from Jasper-net

0 comments: