How to: Force Tests to Stop Running After a Specified Period of Time

You can set time limits with which the execution of a test or a test run will comply. You might need to do this, for example, if you work in a test lab and need a test run to complete by a certain time of day.

Another scenario for the use of time limits is that of non-responsive code. If a test encounters a section of code that does not respond, your use of a test time-out and a test-run time-out lets the individual test finish so that the test run can continue with subsequent tests.

There are two ways to set time limits:

You cannot set test time-outs for specific test types. Test time-outs do not apply to manual tests, but the test-run time-outs are always in effect, even for test runs that contain manual tests.

Setting time limits for tests and test runs has these effects:

  • When the duration of a test run reaches its time-out value, the test run is aborted, even if some tests have not been run.

  • When the duration of an individual test reaches its time-out value, the execution of that test stops, and the test is marked as Timeout. The test run continues with the subsequent test.

How Initialization Methods Affect Test Time-Outs

A test run can include an AssemblyInitializeAttribute method and a ClassInitializeAttribute method. If these methods are defined, they will take a finite amount of time to complete, and then the first test in the test run starts.

The amount of time that the initialization methods use is added to the amount of time that the first test uses. We can express this as a formula that uses these terms:

  • Dt = total duration

  • Dr = amount of time that a test runs

  • I = initialization methods for the assembly and the class

  • Tn = test that runs in a random position within a test run

  • T1 = test that runs in the first position within a test run

The following applies for all tests after the first test:

Dt**[Tn] = Dr[Tn]**

But note the following case:

Dt**[T1] = Dr[T1] + D[I]**

The first test that runs might have a per-test time-out value assigned. In this case, the initialization time counts toward the time limit that is imposed on the first test, and it therefore could cause that test to fail.

Note

This limitation could cause only the first test to fail. Subsequent tests in the test run are unaffected.

If your test run includes initialization methods, you could circumvent this limitation in the following way: Apply to each test a time-out that is greater than the sum of the expected execution times for the test method and any initialization methods. You would have to do this for each test. Because unit testing does not guarantee the order in which tests run, you cannot predict which test will run first.

How Cleanup Methods Affect Test Run Time-Outs

A test run can include an AssemblyCleanupAttribute method and a ClassCleanupAttribute method. After these methods complete, the test run ends.

The test run might have a time-out value assigned. The amount of time that is used by the cleanup methods is added to the total amount of time that is used by the test run. In other words, the cleanup time counts toward the time limit that you have imposed on the test run as a whole, and therefore could cause the test run to time out.

Setting Time-Out period for All Tests

To set the test run time-out for all tests using the tests settings file

  1. Open Solution Explorer.

  2. In the Solution Items folder of your solution, double-click the test settings file.

    The Configuration Settings dialog box appears.

  3. Choose Test Timeouts.

  4. Check Abort a test run if its total execution time exceeds:

  5. Set the test run time-out value, in hours, minutes, and seconds.

  6. Choose Save and then choose Close.

    For this setting to take effect, the test settings that you edited must be the active test settings. For more information, see How to: Select the Active Test Settings from Microsoft Visual Studio.

Override the Per-Test Time-Out Value on Individual test

To set the per-test time-out by using an attribute of a unit test

  1. Open the source code file of a unit test.

  2. Locate the TestMethod attribute of the test.

  3. Add the Timeout attribute and a parameter that expresses the timeout value.

    The time-out value is expressed in milliseconds.

    Example (Visual C#):

    [TestMethod(), Timeout(80)]
    public void MyTestMethod()
    {
    // test code
    }
    

    Example (Microsoft Visual Basic):

    <TestMethod(), Timeout(80)> _
    Public Sub MyTestMethod()
    ' test code
    End Sub
    

    For this test only, the value that you set overrides the per-test time-out value set in the test settings. The test-run time-out value is unaffected.

See Also

Tasks

How to: Select the Active Test Settings from Microsoft Visual Studio