We recommend using Visual Studio 2017

How to: Schedule Test Runs

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

The testing tools of Visual Studio Team System do not contain an explicit feature for scheduling future test runs. However, they do let you organize tests into test lists and test metadata files, which you use when you schedule 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 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 Basic 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 must have Team Explorer installed and you must have a connection to a Team Foundation Server. You would also have to have Team Foundation Server command-prompt commands enabled, and you have to have to have established a workspace. For information, see Walkthrough: Working with Team Foundation Version 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.

    Note the test lists that contain the tests that 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.

  2. Write a batch file that contains the MSTest.exe command to run the tests that 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 that 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 2010\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 Note

      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. Ordinarily, 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 Note

      You might have 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 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; if there is 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 disk and note its location and name.

  5. (Recommended) Run and debug the script to make sure that 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 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. Locate the folder that contains the batch file that 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 that 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.