You can use MsTest.exe to execute Unit, Web, and Load tests from a command line. For more information, please read Sean Lumley also blogged the details on how to run Web, coded Web, and load tests through mstest.exe. This post will show you by creating a wrapper of the mstest process, you can automate Web/Load tests in code.

Step 1 – Create a RunHelper class to wrap up the MsTest.exe process

This helper class constructs the command-line argument list for MsTest.exe, starts the MsTest process to run your tests, and logs the output. You can modify the MSTestArgumentsFormat to use different set of mstest switches.


public class RunHelper


    static readonly string VSPath = Path.Combine(Environment.GetEnvironmentVariable("ProgramFiles"),

                                                  @"Microsoft Visual Studio 10.0\Common7\IDE");

    static readonly string MSTestPath = Path.Combine(VSPath, "MSTest.exe");


    static readonly string MSTestArgumentsFormat =


                "/testcontainer:{0} /testsettings:{1} /resultsfile:{2}",






    public static void RunMSTest(string container, string testsettings, string resultsFileName, int timeout)


        // Build the command line argument

        string commandLineArguments = string.Format(CultureInfo.InvariantCulture,







        ProcessStartInfo startInfo =

            new ProcessStartInfo(





        startInfo.CreateNoWindow = true;

        startInfo.UseShellExecute = false;

        startInfo.RedirectStandardError = true;

        startInfo.RedirectStandardOutput = true;

        startInfo.WindowStyle = ProcessWindowStyle.Hidden;


        // Create and run the process

        Process mstestProcess = Process.Start(startInfo);

        mstestProcess.StartInfo = startInfo;




        string stdout = mstestProcess.StandardOutput.ReadToEnd();

        string stderr = mstestProcess.StandardError.ReadToEnd();



        int exitCode = mstestProcess.ExitCode;



        // log the output

        Console.WriteLine("{0} {1}", MSTestPath, commandLineArguments);

        Console.WriteLine("exitcode: {0}", exitCode);

        Console.WriteLine("stdout: {0}", stdout);

        Console.WriteLine("stderr: {0}", stderr);



Step 2 – Create a TestRunner class to run all the tests

I added three simple functions to the following helper class for running Web, coded Web, and load tests. Basically, you can add your logics to the methods to use different testsettings files in different test environments, e.g. local vs remote, 32-bit mode vs 64-bit mode, running tests with or without data collection, and running tests using different controllers.


public class TestRunner


    // Specify a proper timeout for you testing

    static readonly int TIMEOUT = 120000;


    static public void RunWebTests()








    static public void RunCodedWebTests()








    static public void RunLoadTests()









Step 3 – Run the tests from a console application


The last step is to create a console application, add classes created in step 1 and 2, and call methods in the TestRunner class. In the following code, it just calls the TestRunner to run all the Web tests.


static void Main(string[] args)





And the output is


C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe /testcontainer:e:\webtest
1.webtest /testsettings:e:\local.testsettings /resultsfile:e:\webtest1Result.trx
exitcode: 0
stdout: Microsoft (R) Test Execution Command Line Tool Version 10.0.30208.0
Copyright (c) Microsoft Corporation. All rights reserved.

Loading e:\local.testsettings...
Loading e:\webtest1.webtest...
Starting execution...

Results               Top Level Tests
-------               ---------------
Passed                e:\webtest1.webtest
1/1 test(s) Passed

Test Run Completed.
  Passed  1
  Total   1
Results file:  e:\webtest1Result.trx
Test Settings: Perf Run