• How to run MSTest in a MSBuild script

    Published by giglesias on July 2nd, 2012 3:53 pm under Continuous Integration, TeamCity7

    No Comments

    Hi everyone,

    I would like to show how to run MSTest in a single MSBuild script. Basically what I did is to execute MSTest.exe and pass the test assemblies and the test settings via command line. You should always include the test settings file because it tells if the Code Coverage is enabled, and in which assemblies.

    First of all, in our build script, we have to specify the path to MSTest.exe. We will then create a new target in our build script where we will execute the tests.

    NOTE: Ensure to put this target after compilation so the test assemblies are generated.

    To pass the test assemblies and the test settings to MSTest.exe, we will use the /testcontainer and /testsettings command line options respectively. If you want to include several test assemblies, you have to put as many /testcontainer as assemblies you have. Here is an example:

    <Target Name=”RunTests” DependsOnTargets=”Compilation”>

    <Exec Command=”&quot;$(MSTestPath)\MSTest.exe&quot; /testsettings:{path-to-test-settings-file} /testcontainer:{path-to-test-assembly1} /testcontainer:{path-to-test-assembly2} /testcontainer:{path-to-test-assembly3}”/>

    </Target>

    This will run all the tests in the assemblies that you passed and will generate a TestResults folder with all the information to measure Code Coverage (if you enabled it in the test settings file).

    I hope you find this information useful. Please provide any feedback you consider necessary.

    Thanks!

  • Leave a comment

    Your email address will not be published.