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.
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 Build Definition.
Prerequisites: If you are using Team Foundation version 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 Version Control from the 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
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.
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:
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>\
(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:
This step is required only if you are using Team Foundation version 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.
Re-build the solution.
msbuild <solution name>.sln
You may need to repeat steps 2b and 2c if you want to include more than one solution in your test pass.
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:
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.
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.
(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.
Save the batch file to your hard drive and note its location and name.
(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
Click Start and then click Control Panel.
In the Control Panel, double-click Scheduled Tasks.
The Scheduled Tasks window opens.
Double-click Add Scheduled Task.
The Scheduled Task Wizard opens.
Click Next and then click Browse.
The Select Program to Schedule dialog box appears.
Navigate to the folder that contains the batch file you wrote.
Click the batch file and then click Open.
(Optional) Change the name of the scheduling task that you are creating.
Under Perform this task, select the frequency with which you want the batch file to run, and then click Next.
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.
Type your user name and password, and then click Next.
The new task appears in the Scheduled Tasks window.