|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
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.
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
describes how to mark a unit test method with the 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 inand , you can use the Test Manager window 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 Manager window, 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 Manager window.
To disable tests
To disable one test, in Test Manager, 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 Test Manager or Test View and press F4 to display the Properties window. Then, set the value of the Test Enabled property to False.
To enable tests
To enable one test, in Test Manager, 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 Test Manager or Test View 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
Open the unit test to edit its source code. To do this, in the Test Manager window, double-click the unit test.
The source code file that contains the unit test opens.
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.