How to: Group and Run Automated Tests Using Test Categories
Test categories let you run groups of tests based on their assigned categories without the requirement to maintain test lists. A test category is a test method attribute that you can assign to one or more tests.
You can use logical operators with test categories to run tests from multiple categories together or to limit the tests that you run to tests that belong to multiple categories. Also, test categories are easy to add as you create your test methods and you do not have to maintain test lists after you have created your test methods.
Visual Studio Enterprise, Visual Studio Test Professional
To manually add test categories to a test
In your unit test project, or coded UI test project in Solution Explorer, open the file that contains the unit test, and then locate the unit test method that you want to change.
Directly above the test method declaration, add a [TestCategory()] attribute for each test category that you want to assign to the test. Separate each attribute by using a comma.
Add the category name enclosed in parentheses to each [TestCategory()] attribute. The following example is a method with three Test Categories assigned to it named "Nightly", "Weekly", and "ShoppingCart":
When you run tests from the command line, you can also use logical operators & (AND), | (OR) and !(NOT) to select the tests to run based on the categories assigned to the tests.
To run test using categories from the command-line
Open a Visual Studio command prompt. (Go to Start, All Programs, Microsoft Visual Studio, Visual Studio Tools, Developer Command Prompt.)
By default, the Visual Studio command prompt opens to the following folder:
<drive letter>:\Program Files\Microsoft Visual Studio 12.0\VC
To change the folder to which the command prompt window opens by default, choose Start, point to Microsoft Visual Studio, point to Visual Studio Tools, right-click Developer Command Prompt, and then choose Properties. In the Developer Command Prompt Properties dialog box, you can change the path of the default folder in the Start in box.
Either change the directory to the location in your solution folder where the test container is located, typically the test project's .dll file, or, when you run the MSTest.exe program in step 3, specify a full or relative path for the test container.
To identify your solution folder, first identify the Visual Studio Projects folder. To do this, choose Options on the Tools menu in Visual Studio, and then choose Projects and Solutions. Under Visual Studio projects location, you see a path such as the following:
<drive letter>:\Documents and Settings\<user name>\My Documents\Visual Studio\Projects
Your solution folder is typically a child of this Projects folder, such as the Bank folder in the following example:
<drive letter>:\Documents and Settings\<user name>\My Documents\Visual Studio\Projects\Bank
To run tests that are assigned to the "Nightly" category, run the VSTest.Console.exe using the /TestCaseFilter option, or from MSTest.exe by using the /testcontainer and the /category options:
Vstest.console.exe myTestProject.dll /TestCaseFilter:TestCategory=Nightly
mstest /testcontainer:MyTestprojectName.dll /category:"Nightly"
The results and summary are displayed in the command prompt window.
You can use either AND or OR in your expression to select categories of tests, but not both in the same expression.
For more information, see VSTest.Console.exe command-line options, Run automated tests from the command line using MSTest and MSTest.exe command-line options.