Dela via


How to: Run Automated Tests from the Command Line Using MSTest

You use the executable program MSTest.exe to run tests from the command line. This program can run any tests that can be automatically run, that is, any tests other than manual tests.

To run tests 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 Command Prompt (2010).

    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 Command Prompt (2010), and then click Properties. In the Visual Studio Command Prompt (2010) Properties dialog box, you can change the path to the default folder in the Start in box.

  2. Either change directory to your solution folder or, when you run the MSTest.exe program in step 3, specify a full or relative path to the metadata file or to 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. Run the MSTest.exe program.

    When you run MSTest.exe, you must specify either a test metadata file or a test container, using either the /testmetadata option or the /testcontainer option, respectively. You use the /testmetadata option only once, to indicate one test metadata file. You can use the /testcontainer option multiple times, to indicate multiple test containers.

    If necessary, include the path to the folder in which the metadata file or test container resides. Test metadata files reside in the solution folder.

    Depending on the test type, test containers are XML files, assemblies built from test projects, or other files that reside in the folders of a test project.

Examples

You must use the /testcontainer option together with the /category option to select which tests in which categories to run. The following command, for example, is run in the solution folder and runs the tests that are in both the Priority 1 and ShoppingCart categories.:

MSTest /testcontainer: testproject2\bin\debug\testproject2.dll /category:"Priority1&ShoppingCart"

Note

Because the test assembly file resides in a different folder, a relative path is necessary,

If you are using test lists, it is best to use the /testmetadata option together with the /testlist option. The following command, for example, is run in the solution folder. Because the test metadata file also resides in that folder, no path is necessary:

MSTest /testmetadata:Bank.vsmdi /testlist:balancetests

Optionally, you can use other MSTest command-line options for purposes such as specifying individual tests to run, publishing test data, and customizing screen output. For more information about these options, see MSTest.exe Command-Line Options.

See Also

Tasks

How to: Obtain Command-Line Test Results

Walkthrough: Using the Command-line Test Utility

Concepts

MSTest.exe Command-Line Options

Command-Line Options for Publishing Test Results