Udostępnij za pośrednictwem


How to: Run Selected Tests

The Team System testing tools offer several ways to run 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 Command-Line Test Execution.

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.

In the following procedures, you select items sometimes by highlighting and sometimes by selecting check boxes. For more information, see Selecting Tests.

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.

Run Tests from Your Source Code Files

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

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

  • 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 focus.

    - or -

  3. In the Test List Editor, check 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 checked in the Test List Editor.

    Note

    To run the tests that are selected in the Test List Editor, the Test List Editor must have 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 checked in the Test Results window. For more information about rerunning tests, see How to: Rerun a Test.

    Note

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

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, check 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 click Run Checked Tests.

    - or -

  • In the test list pane of the Test List Editor, put a check mark by 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 click Run Checked Tests.

    - or -

  • In the Test Results window, with one or more test results checked, 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 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 and smart device 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

Other Resources

Command-Line Test Execution