Udostępnij za pośrednictwem


How to: Disable and Enable Tests

You can disable a test to make it temporarily unavailable when you run a group of tests that contains it. You can re-enable it at any time. Why would you disable a test? Consider the following possibilities:

  • Your product build contains a bug that causes a crash. This bug is located in code that one of your tests exercises. This test is one of many that are run in sequence. Because of the crash that this test indirectly triggers, your test run cannot move past that test in the sequence. The solution is to file a bug on the code that crashes, and disable the test until the bug is fixed. Then, the test sequence can progress beyond that one test.

  • A specific test in a test run uncovers a bug, which you file. To keep from repeatedly finding the same known failure every time that you run the test selection, disable the test.

    Note

    Remember to re-enable a test when its associated bug is fixed.

If you run a test list that contains disabled tests, only the enabled tests within that test list, if any, will be run. If a test list does not contain any enabled tests, the test list itself is disabled.

Using the Ignore Attribute with Unit Tests

Using the Ignore Attribute describes how to mark a unit test method with the IgnoreAttribute attribute. Using this attribute changes the availability of the test, but it differs in the following ways from the other way to disable and enable tests:

  • As described in Disabling Tests and Enabling Tests, you can use the Test List Editor to disable and enable tests of any type. But the Ignore attribute can be applied only to a test method. This means that you can apply the Ignore attribute only to unit tests.

  • The flag for enabling and disabling tests is stored in the test metadata file.

  • The Ignore attribute for a unit test resides in the source file of the unit test, together with the other attributes and source code of the test. This means that, if you disable a unit test in the Test List Editor, and later run the test by using the MSTest.exe command-line utility, the test still runs. But if you mark a unit test with the Ignore attribute, compile the assembly, and then run the test by using MSTest.exe, the test does not run. It does not run because the Ignore attribute has become part of the test.

These two approaches are used in different scenarios:

  • Ignore attribute. You are a developer who authors both production code and test code. You are in the middle of making a large set of changes, and you know that one of your unit tests will be broken for a while. Therefore, when you check in the test, you mark it with the Ignore attribute to indicate that you do not expect it to work.

  • Disable and enable. You work in a build lab and you do not have access to the source code of the tests you are running. In this case, you can use only the enable and disable capabilities of the Test List Editor.

Disabling Tests

To disable tests

  • To disable one test, in the Test List Editor, right-click an enabled test and then click Disable. To disable multiple tests, highlight the enabled tests you want to disable, by pressing CTRL or SHIFT when clicking the tests' rows. Next, right-click the selected rows and then click Disable.

    - or -

    Select the test in the Test List Editor or the Test View window and press F4 to display the Properties window. Then, set the value of the Test Enabled property to False.

Enabling Tests

To enable tests

  • To enable one test, in the Test List Editor, right-click a disabled test and then click Enable. To enable multiple tests, highlight the disabled tests you want to enable by pressing CTRL or SHIFT when clicking the tests' rows. Next, right-click the selected rows and then click Enable.

    - or -

    Select the test in the Test List Editor or the Test View window and press F4 to display the Properties window. Then, set the value of the Test Enabled property to True.

Using the Ignore Attribute

To use the Ignore attribute

  1. Open the unit test to edit its source code. To do this, in the Test List Editor, double-click the unit test.

    The source code file that contains the unit test opens.

  2. Add the IgnoreAttribute attribute to the unit test.

    The test is disabled. In the Properties window, the Test Enabled property is automatically set to False. To learn more about the scope of this action, see the previous section "Using the Ignore Attribute with Unit Tests."

    To re-enable the test, delete or comment out the Ignore attribute.

See Also

Tasks

How to: Run Selected Tests

Reference

IgnoreAttribute