How to: Schedule Test Runs

The testing tools of Visual Studio Team System do not contain an explicit feature for scheduling future test runs. They do, however, let you organize tests into test lists and test metadata files, which you use when scheduling test runs by other means. This topic describes how to use Windows Task Scheduler to run a batch file that starts a test run.

Note

Running tests is one option that you can specify when you create a build type using Team Foundation Build. You can then use the command tbuild to run that build type from a command line. In a way similar to the steps in the following procedure, you could write a batch file that uses the tbuild command to start a build, and then run that batch file on a regular schedule using the Windows Task Scheduler. For more information, see How to: Create a New Build Type.

Schedule Tests using Windows Task Scheduler

Prerequisites: If you are using Team Foundation source control and you want to test only the latest versions of your source files, you need Team Explorer installed and you need a connection to a Team Foundation Server. You would also need to have Team Foundation Server command-prompt commands enabled, and you need to have established a workspace. For information, see Walkthrough: Working with Team Foundation Source Control from Command Line.

This procedure has two parts. In the first part, you write a batch file that synchronizes your solution workspace and runs tests in your solution. In the second part, you schedule that batch file to run at regular intervals.

To write a batch file that runs tests

  1. Create the tests that you want to run. Optionally, add them to test lists. For more information, see Using Test Lists.

    Take note of the test lists that contain the tests you want to run, and the test metadata files that contain those test lists. Solution Explorer displays the test metadata file of your solution; the file has the extension .vsmdi. For more information about test lists and test metadata files, see Reusing Tests.

  2. Write a batch file that contains the MSTest.exe command to run the tests you want. For information about the MSTest.exe command, see MSTest.exe Command-Line Options.

    This batch file accomplishes several tasks:

    1. Change to the folder of the project you are testing.

      cd <solution folder>

      Solution folders are usually found under the Products folder in the Visual Studio installation, such as in the following example: C:\Documents and Settings\<user name>\My Documents\Visual Studio 2005\Projects\<solution name>\

    2. (Optional) Get the latest version of your source files and your test files.

      The following command synchronizes only the current subtree:

      tf get . /r

      Or use the following command, which synchronizes your entire solution workspace:

      tf get

      Note

      This step is required only if you are using Team Foundation source control and you want to test only the latest versions of your source files. Reasons to reconsider using this synchronization step are that it can introduce merge conflicts, which require user action, or cause build breaks against local changes. More commonly, as a developer or tester, you schedule nightly test runs on your own development or test computer to test the source files present on that computer on the build that you have installed.

    3. Re-build the solution.

      msbuild <solution name>.sln

      Note

      You may need to repeat steps 2b and 2c if you want to include more than one solution in your test pass.

    4. Run the tests.

      The following command runs tests only in one specific test metadata file. To specify a test list, use the /testlist option in combination with the /testmetadata option.

      MSTest /testcontainer:<test project name>\bin\debug\TestProject.dll

      Or use the following command, which runs tests in an entire test metadata file:

      MSTest /testmetadata:TestProject1.vsmdi

      Note

      Before adding an MSTest command to the batch file, you should run the command on a command line to verify that it runs the tests that you want in the way that you expect.

      For more information about test metadata files and test lists, see Reusing Tests.

      Note

      If you have pending changes on your local workspace, the tf command will not synchronize any changes to those files. If conflicts arise, a dialog box is displayed. You can suppress dialog boxes and error messages by using the /noprompt option with the tf.exe get command; in the case of a merge conflict, the local copy of the file is retained.

  3. (Optional) Publish the results of the test run by adding /publish* options of the MSTest.exe command. For more information, see Command-Line Options for Publishing Test Results.

  4. Save the batch file to your hard drive and note its location and name.

  5. (Recommended) Run and debug the script to make sure your tests run.

    You have created the batch file that runs your tests. Now, you can program Windows Task Scheduler to run it regularly, by performing the steps in the following procedure.

To schedule a batch file to run at regular intervals

  1. Click Start and then click Control Panel.

  2. In the Control Panel, double-click Scheduled Tasks.

    The Scheduled Tasks window opens.

  3. Double-click Add Scheduled Task.

    The Scheduled Task Wizard opens.

  4. Click Next and then click Browse.

    The Select Program to Schedule dialog box appears.

  5. Navigate to the folder that contains the batch file you wrote.

  6. Click the batch file and then click Open.

  7. (Optional) Change the name of the scheduling task that you are creating.

  8. Under Perform this task, select the frequency with which you want the batch file to run, and then click Next.

  9. Depending on the frequency you selected, the next page might require you to provide more detail about the times you want the batch file to run, such as the start time and start date. When you are finished, click Next.

  10. Type your user name and password, and then click Next.

  11. Click Finish.

    The new task appears in the Scheduled Tasks window.

See Also

Tasks

How to: Create a New Build Type

Reference

Get Command

Concepts

MSTest.exe Command-Line Options
Reusing Tests

Other Resources

Tf Command-Line Utility Commands