Поделиться через


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 using the Test List Editor.

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.

The following procedures describe how to create, assign, filter, and run test categories with your tests.

Creating and Assigning Test Categories

To create a new test category

  1. On the Test menu, click Windows and then select Test View.

    The Test View window is displayed.

  2. Select a test.

  3. In the properties pane of the selected test, click () in the Test Categories column.

    The Test Category window is displayed.

  4. In the Add New Category field, type the name of your new test category.

  5. Click Add.

  6. Click OK.

    The new test category is assigned to your test and it will be available to the other tests in the Test View window.

To assign test categories to a test

  1. On the Test menu, click Windows and then select Test View.

    The Test View window is displayed.

  2. Select a test.

  3. In the properties pane of the selected test, click () in the Test Categories column.

    The Test Category window is displayed.

  4. Select the test category from the Available Categories pane that you want to assign to the test. To select several test categories, press Shift + M.

  5. Click >>.

    The categories that you selected will now appear in the Assigned Categories pane.

  6. Click OK.

To manually add test categories to a test

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

    - or -

    In Test View, double-click the unit test and scroll to the unit test method.

  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()
    {
    }
    

Removing Test Categories

To remove test categories from a test

  1. On the Test menu, click Windows and then select Test View.

    The Test View window is displayed.

  2. Select a test.

  3. In the properties pane of the selected test, click () in the Test Categories column.

    The Test Category window is displayed.

  4. Select the test category from the Assigned Categories pane that you want to remove from the test. To select several test categories press Shift + M.

  5. Click <<.

    The categories that you selected will now appear in the Available Categories pane.

  6. Click OK.

Filtering Tests by Categories

To sort tests in Test View window by test categories

  1. On the Test menu, click Windows and then select Test View.

    The Test View window is displayed.

  2. On the toolbar for Test View, click the arrow in the left drop-down list and select Test Categories.

    The default is [All Columns].

  3. In the filtering text box type the name of the category that you want to filter on and press Enter.

    The tests associated with the category that you specified are displayed in Test View.

    The default is <Clear Filter>.

  4. (Optional) Click the Test Name or Project column headers to sort the list.

Running Tests by Categories

To run test using categories in Test View

  1. On the Test menu, click Windows and then select Test View.

    The Test View window is displayed.

  2. In the toolbar for Test View, click the arrow in the left drop-down list and select Test Categories.

    The default is [All Columns].

  3. In the filtering text box, type the name of the category that you want to filter on and press Enter.

    The tests associated with the category that you specified are displayed in Test View.

    The default is <Clear Filter>.

    Note

    You cannot use logical operators with test categories when you filter tests that you want to run from the Test View. You can only do this when you run tests from the command line.

  4. (Optional) Click the Test Name or Project column headers to sort the list.

  5. Click the tests in the test category that you want to run.

  6. Use SHIFT + CLICK or CNTRL + CLICK to select additional tests.

  7. Click Run Selection on the toolbar.

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, click Start, point to All Programs, point to Microsoft Visual Studio 2010, point to Visual Studio Tools, and then click Visual Studio 10.0 Command Prompt.

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

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

    Note

    To change the folder to which the command prompt window opens by default, click Start, point to Microsoft Visual Studio 2010, point to Visual Studio Tools, right-click Visual Studio 10.0 Command Prompt, and then click Properties. In the Visual Studio 10.0 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, click Options on the Tools menu in Visual Studio, and then click 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 both the "Nightly" and "ShoppingCart" categories, run the MSTest.exe program by using the /testcontainer and the /category switches:

    mstest /testcontainer:MyTestprojectName.dll /category:"Nightly&ShoppingCart"

    The results and summary are displayed in the command prompt window.

    Note

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

See Also

Concepts

Defining Test Categories to Group Your Tests