if ([string]::IsNullOrEmpty($applicationPath) -and [string]::IsNullOrEmpty($BuildDefinition) -and [string]::IsNullOrEmpty($BuildNumber)) { $(throw "The build definition and build number or buid directory parameters are required.") } # Find tcm.exe $vscommtools = $env:VS120COMNTOOLS if ($vscommtools -eq $null) { # Visual Studio 2012 $vscommtools = $env:VS110COMNTOOLS } if ($vscommtools -eq $null) { # Visual Studio 2010 $vscommtools = $env:VS100COMNTOOLS } $tcmExe = [System.IO.Path]::GetFullPath($vscommtools + "..\IDE\TCM.exe") if (![System.IO.File]::Exists($tcmExe)) { $(throw "The path to tcm.exe could not be found.") } # Find temp folder to store the test results $tempFolder = $env:TEMP if ($tempFolder -eq $null) { $tempFolder = $env:TMP } if (![System.IO.Directory]::Exists($tempFolder)) { $(throw "The temp folder could not be found.") } "Executing with the following parameters:" " TCM Executable: $tcmExe" " Build Directory: $applicationPath" " Build Definition: $BuildDefinition" " Build Number: $BuildNumber" " Test Environment: $TestEnvironment" " Collection: $Collection" " Team project: $TeamProject" " Plan ID: $PlanId" " Suite ID: $SuiteId" " Configuration ID: $ConfigId" " Title: $Title" " Settings Name: $SettingsName" " Inconclusive result fails tests: $InconclusiveFailsTests" " Remove /include parameter from /create command: $RemoveIncludeParameter" " Test run wait delay: $TestRunWaitDelay" " Temp folder: $tempFolder" # Build Arguments # Using lots of arguments can be tricky. It is much easier to build # in parts so options will simply by "" $exitCode = 1 $argplanid = "/planid:$PlanId" $argtitle = "/title:$Title" $argsuiteid = "/suiteid:$SuiteId" $argconfigid = "/configid:$ConfigId" $argcollection = "/collection:$Collection" $argteamproject = "/teamproject:$TeamProject" if(![string]::IsNullOrEmpty($applicationPath)) { $argbuilddir = "/builddir:$applicationPath" $argbuild = "" $argbuilddefinition = "" } else { $argbuilddir = "" $argbuild = "/build:$BuildNumber" $argbuilddefinition = "/builddefinition:$BuildDefinition" } "`nCreating test run ..." $testRunId = & $tcmExe 'run' '/create' $argplanid $argtitle $argsuiteid $argconfigid $argteamproject $argcollection $argbuild $argbuilddefinition $argbuilddir if ($testRunId -match '.+\:\s(?\d+)\.') { # Use the results of the regular expression to find the testRunId $testRunId = $matches.TestRunId # Build the final arguments needed $argid = "/id:$testRunId" $testRunResultsTrxFileName = $tempFolder + "\TestRunResults$testRunId.trx" $argquery = "/querytext:SELECT * FROM TestRun WHERE TestRunId=$testRunId" $argresult = "/resultsfile:""$testRunResultsTrxFileName""" "Waiting for test run $testRunId to complete ..." $waitingForTestRunCompletion = $true while ($waitingForTestRunCompletion) { Start-Sleep -s $TestRunWaitDelay $testRunStatus = & $tcmExe 'run' '/list' $argteamproject $argcollection $argquery if ($testRunStatus.Count -lt 3 -or ($testRunStatus.Count -gt 2 -and $testRunStatus.GetValue(2) -match '.+(?\d+[/]\d+[/]\d+)')) { $waitingForTestRunCompletion = $false } } "Evaluating test run $testRunId results..." # We do a small pause since the results might not be published yet. Start-Sleep -s $TestRunWaitDelay $exportResult = & $tcmExe 'run' '/export' $argid $argresult $argcollection $argteamproject if ([System.IO.File]::Exists($testRunResultsTrxFileName)) { # Load the XML document contents. [xml]$testResultsXml = Get-Content "$testRunResultsTrxFileName" # Extract the results of the test run. $total = $testResultsXml.TestRun.ResultSummary.Counters.total $passed = $testResultsXml.TestRun.ResultSummary.Counters.passed $failed = $testResultsXml.TestRun.ResultSummary.Counters.failed $inconclusive = $testResultsXml.TestRun.ResultSummary.Counters.inconclusive # Output the results of the test run. "`n========== Test: $total tests ran, $passed succeeded, $failed failed, $inconclusive inconclusive ==========" # Determine if there were any failed tests during the test run execution. if ($failed -eq 0 -and (-not $InconclusiveFailsTests -or $inconclusive -eq 0)) { # Update this script's exit code. $exitCode = 0 } # Remove the test run results file. [System.IO.File]::Delete($testRunResultsTrxFileName) | Out-Null } else { "`nERROR: Unable to export test run results file for analysis." } } if ($exitCode -gt 0) { "`nERROR: Operation failed with error code $exitCode." } "`nDone." exit $exitCode