Share 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 a test run configuration. For more information about test run configurations, see How to: Specify a Test Run Configuration and How to: Apply a Test Run Configuration. As with all run configuration settings, time-out settings are in effect whenever you run tests while that run configuration is active.

  • 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. These methods take a finite amount of time to complete, and then the first test in the test run starts.

The first test that runs might have a per-test time-out value assigned. The amount of time that the initialization methods use is added to the amount of time that the first test uses. In other words, 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 run configuration file.

    The test run configuration dialog box opens.

  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 run configuration that you edited must be the active run configuration. For more information, see How to: Apply a Test Run Configuration.

To set the per-test time-out by using test run configuration

  1. Open Solution Explorer.

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

    The test run configuration dialog box opens.

  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 run configuration that you edited must be the active run configuration. For more information, see How to: Apply a Test Run Configuration.

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

  1. Open Test Manager or Test View.

  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 you set overrides the per-test time-out value set in the test run configuration. The test-run time-out value is unaffected.

See Also

Tasks

How to: Specify a Test Run Configuration
How to: Apply a Test Run Configuration