Dela via


How to: Set Time Limits for Running Tests

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 set time-out values for tests and test runs in test settings. For more information about test settings, see Create Test Settings to Run Automated Tests from Visual Studio.

  • You can override the per-test time-out value by setting a property on an individual test. You cannot override the test-run time-out value.

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.

To set the test run time-out

  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. Click 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. Click Save and then click 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: Apply Test Settings from Microsoft Visual Studio.

To set the per-test time-out by using test settings

  1. Open Solution Explorer.

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

    The Test Settings dialog box appears.

  3. Click Test Timeouts.

  4. Check Mark an individual test as failed if its execution time exceeds:

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

  6. Click Save and then click 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: Apply Test Settings from Microsoft Visual Studio.

To set the per-test time-out by using a test property

  1. Open the Test List Editor or the Test View window.

  2. Click the test and then press F4.

    The Properties window opens, and the properties for that test are displayed.

  3. For the Timeout property, set the value, in hours, minutes, and seconds.

    Note

    The time-out value for a manual test is Infinite. You cannot change this value.

    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.

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: Apply Test Settings from Microsoft Visual Studio