Share via

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 Ultimate, Visual Studio Premium, Visual Studio Test Professional

Creating and Assigning Test Categories

To manually add test categories to a test

  1. 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.

  2. 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.

  3. 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":

    [TestCategory("Nightly"), TestCategory("Weekly"), TestCategory("ShoppingCart"), TestMethod()]
    public Void DebitTest()

Running Tests by Categories

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

  1. Open a Visual Studio command prompt.

    To do this, choose Start, point to All Programs, point to Microsoft Visual Studio 2012, point to Visual Studio Tools, and then choose Developer Command Prompt.

    By default, the Visual Studio command prompt opens to the following folder:

    <drive letter>:\Program Files\Microsoft Visual Studio 11.0\VC


    To change the folder to which the command prompt window opens by default, choose Start, point to Microsoft Visual Studio 2012, 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.

  2. 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

  3. 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, How to: Run Automated Tests from the Command Line Using MSTest and MSTest.exe Command-Line Options.

See Also


Defining Test Categories to Group Your Tests