Thursday, June 4, 2015

PowerShell and MTM - Scheduling a daily test execution using TCM

You can run automated tests that are part of a test plan from the command line using the TCM utility. This lets you start a test run using a PowerShell script that can be included as part of your post build script or schedule a nightly run. TCM.EXE is located in \Program Files\Microsoft Visual Studio 11.0\Common7\IDE on any computer that has Visual Studio Ultimate, Visual Studio Premium, or Visual Studio Test Professional installed. I've created a sample script to invoke TCM to create a test run targeting a test plan and a suite so that I can use this script to schedule a run from my TFS build daily. My test cases are created and maintained in Microsoft Test Manager and I can use the capabilities of the TCM executable to create a scheduled run of the tests in MTM.

The tcm run /create command is being utilized to achieve this. The complete options available for the run /create option is as
tcm run /create /title:title /planid: id /collection:CollectionURL /teamproject:project (suiteid:id /configid:configid | /querytext: query) [/settingsname:name] [/owner:owner] [/builddir:directory]  [/testenvironment:name] [/login:username,[password]] [/include]

And my module looks like:

Function Get-VSPath
{
       $vsInstallationPath = [String]::Empty
      
       if($env:VS120COMNTOOLS -ne $null)
       {
              $vsInstallationPath = $env:VS120COMNTOOLS.Replace("\Tools", "\IDE")
       }
       elseif($env:VS110COMNTOOLS -ne $null)
       {
              $vsInstallationPath = $env:VS110COMNTOOLS.Replace("\Tools", "\IDE")
       }     
       else
       {
              $vsInstallationPath = $env:VS100COMNTOOLS.Replace("\Tools", "\IDE")
       }
      
       if($vsInstallationPath -eq [String]::Empty)
       {
              throw "You need to have TCM executable installed to continue."      
       }
       return $vsInstallationPath
}

Function Invoke-TfsTestRun
{
    param
    (
        [Parameter(Mandatory=$true)]
        [String] $TFSCollectionUrl,

        [Parameter(Mandatory=$true)]
        [String] $TeamProject,

        [Parameter(Mandatory=$true)]
        $PlanId,

        [Parameter(Mandatory=$true)]
        $SuiteId,

        [Parameter(Mandatory=$true)]
        $ConfigId
    )

    $vsLocation = Get-VSPath
    & "$vsLocation\TCM.exe" @("run", "/create", "/title:DailyRun", "/planid:$PlanId", "/suiteid:$SuiteId", "/configid:$ConfigId", "/collection:$TFSCollectionUrl", "/teamproject:$TeamProject")
}

Export-ModuleMember -Function Invoke-TfsTestRun

PS C:\PSDemo> Invoke-TfsTestRun -TFSCollectionUrl http://pradev.tfsser:8080/tfs/defaultcollection -TeamProject TEAMPROJ01 -PlanId 2 -SuiteId 14 -ConfigId 23
Run created with ID: 26553.




No comments: