Share via


How to: Run Automated Tests from Microsoft Visual Studio

This topic is about how to use Microsoft Visual Studio 2010 to run automated tests. You can run automated tests from both the Visual Studio integrated development environment (IDE) and at a command prompt. For more information about how to run tests at a command prompt, see Running Automated Tests from the Command Line.

Note

When you run one or more tests in Visual Studio, if the test contents are new or have been changed but not saved, they are automatically saved before the test is run. Similarly, if the code of a unit test has been edited but the project that contains the test has not been re-built, Visual Studio builds the project before you run the test.

However, if you want to plan out your testing effort and run your tests as part of a test plan, you can use Microsoft Test Manager. For more information about how to use Microsoft Test Manager, see Defining Your Testing Effort Using Test Plans.

Note

Microsoft Test Manager is provided as part of Visual Studio 2010 Ultimate and Visual Studio Test Professional products.

Flexibility in Running Tests

Visual Studio provides many different ways to run tests. You can choose the way that best suits your current needs:

  • Run Tests From Your Source Code Files. By using the keyboard, you can run tests from any text-based file in your solution. In particular, you can run tests while editing a file that contains your code under test. This lets you change source code and immediately test it without using a window or a menu.

  • Run Tests From Files in Your Test Project. By using the mouse or the keyboard, you can run tests from the file that contains your test code. This lets you change a test and then run it immediately without using a window or a menu.

  • Run Tests by Using Testing Tools Windows. You can also run tests from the Test View window, the Test List Editor, and from the Test Results window.

Note

After you run a test in Microsoft Visual Studio 2010, the results of all the tests that were executed in that run are saved automatically on your computer in a test run file. How many test runs are saved depends on a setting in the Options dialog box.

Run Tests In a Specific Order

You can also run tests in a specific order if you create an ordered test. For more information about ordered tests, see Setting Up Your Test Run Sequence Using Ordered Tests.

Run Tests from Your Source Code Files

To run tests from source code files in your solution, by using the keyboard

  1. In Visual Studio, open a source code file anywhere in your solution.

  2. You can use the following keyboard shortcuts to run tests from that file.

    Keyboard Shortcut

    Description

    CTRL + R, then press A

    Runs all the tests in all test projects.

    CTRL + R, then press D

    Runs all tests that were run in the last test run.

    CTRL + R, then press F

    Runs all tests in the last test run that did not pass.

Note

You can use these shortcuts in your source code file that contains the test methods, and in the Test View window and the Test List Editor window.

Run Tests from Files in Your Test Project

To run tests from your test code file, by using the keyboard

  1. In Visual Studio, open the source-code file that contains your test methods.

  2. Click to define the testing scope: Place the cursor in a test method, in a test class, or outside the scope of a test class.

  3. You can use the following keyboard shortcuts to run tests based on that scope.

Keyboard Shortcut

Tests Run

CTRL + R, then press T

This runs the tests in the current scope. That is, it runs the current test method, all the tests in the current test class, or all the tests in the namespace, respectively.

CTRL + R, then press C

This runs all the tests in the current test class.

CTRL + R, then press N

This runs all tests in the current namespace.

To run tests from your test code file, by using the mouse

  1. In Visual Studio, open the source-code file that contains your test methods.

  2. Right-click in a test method, in a test class, or outside the scope of a test class, and then click Run Tests.

    This command runs the tests in the current scope. That is, it runs the current test method, all the tests in the current test class, or all the tests in the current namespace, respectively.

Run Tests by Using Testing Tools Windows

To run tests by using the testing tools windows, by using the keyboard

  1. In the Test View window, select one or more tests.

  2. Press CTRL+R and then press T (or CTRL+R, C, or CTRL+R, N).

    This runs all the tests that are selected in the Test View window.

    Note

    To run the tests that are selected in the Test View window, the Test View window must have the focus.

    - or -

  3. In the Test List Editor, select one or more tests.

  4. Press CTRL+R and then press T (or CTRL+R, C, or CTRL+R, N).

    This runs all the tests that are selected in the Test List Editor.

    Note

    To run the tests that are selected in the Test List Editor, the Test List Editor must have the focus.

    - or -

  5. In the Test Results window, select the check box next to one or more tests, or leave the check boxes selected after a test run.

  6. Press CTRL+R and then press T (or CTRL+R, C or CTRL+R, N).

    This reruns all the tests that are selected in the Test Results window. For more information about rerunning tests, see How to: Rerun a Test.

    Note

    When you rerun tests from a test result window, the test settings that are associated with the test result are used when you rerun the test. The test settings associated with the solution that contains the test project are not used when you rerun from the test result window.

To run tests by using the testing tools windows, by using the mouse

  • Right-click the test in the Test View window, and then click Run Selection.

    - or -

  • Select the test in the Test View window, and then click Run Selection on the Test View toolbar.

    - or -

  • In the Test List Editor, select one or more tests. Then, either

    • click Run Checked Tests on the Test List Editor toolbar

      - or -

    • right-click the surface of the window and then click Run Checked Tests.

    - or -

  • In the test list pane of the Test List Editor, select the check box next to a test list that contains the test or tests that you want to run. Then, click Run Tests on the Test List Editor toolbar or right-click the surface of the window and then click Run Checked Tests.

    - or -

  • In the Test Results window, with one or more test results selected, click the Run button or click Rerun original tests on the Test Results window status bar. For more information, see How to: Rerun a Test.

To run tests by using categories in Test View

  1. In the toolbar for Test View, click the arrow in the left drop-down list.

    The default is [All Columns].

  2. Select Test Categories.

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

    The default is <Clear Filter>.

  4. Press Enter.

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

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

  6. Click the tests within the test category that you want to run.

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

  8. Click Run Selection on the toolbar.

To run a test project

  1. Your test project must be the startup project in your solution. To do this, in Solution Explorer, right-click the test project and then click Set as StartUp Project.

    Solution Explorer indicates the test project as the startup project by displaying its name in bold text.

  2. Start your project. Do this by selecting Start Debugging or Start Without Debugging on the Debug menu, or by pressing the corresponding key combinations, F5 and CTRL+F5. You can also do this by clicking Start Debugging (F5) on the Standard toolbar.

    All the tests in the test project are run.

    Note

    The Start Debugging option runs but does not debug tests that do not run under the Default host, such as ASP.NET unit tests. For more information, see How to: Debug while a Test is Running.

See Also

Tasks

How to: Rerun a Test

How to: Debug while a Test is Running

Concepts

Running Automated Tests from the Command Line