0

I have a PowerShell script to kick off an automated test run using command-line interface (tcm.exe) for Microsoft Test Manager.

I also then have a cleanup Powershell script associated with the test run (in the .testsettings file), which exports the test result (tcm.exe run /export), but my problem is that I need the test run ID. It is output from the 'tcm.exe run /create' command, but it's of no use because first of all, it outputs as "Run created with ID: 501", and second of all because the /create command is run from a separate PowerShell script.

I can use the tcm.exe run /list to get a list of ALL the test ID's, but this is useless, as I only need the one most recent test run.

Any ideas, anyone?

chaliasos
  • 9,659
  • 7
  • 50
  • 87
Ciaran Gallagher
  • 3,895
  • 9
  • 53
  • 97
  • What's wrong with the way it outputs? You could extract the ID with regex ".+(\d*)". And what do you mean by "separate PowerShell script"? Are all of the functions ran in one session? As in, open PShell, call script 1, then 2, then 3...or is it open PShell, call script 1, close PShell, open new PShell, call script 2...? – SpellingD Aug 06 '12 at 14:46
  • The PowerShell script launches a test run, then closes. Meanwhile, the test run launches on a remote environment. At the end of that test run, it launches a script to export the test results, then reads the result file and sends an email out to report the result. – Ciaran Gallagher Aug 06 '12 at 14:56
  • Since you say that at the end of the run it launches a script, I assume that the test run is wrapped inside of a script, so you should be able to capture the output and grab the ID from what runs the test, and pass it to the next script as an argument. – SpellingD Aug 06 '12 at 15:15
  • See that's the problem. Microsoft Test Manager doesn't allow arguments to be passed into scripts, and even if they did it still would not work as the setup/cleanup scripts are configured from the .testsettings file, so we wouldn't be able to access the ID variable. – Ciaran Gallagher Aug 06 '12 at 15:35
  • So I implemented a solution - I captured the output from tcm /create into a variable, manipulated the string to retrieve just the ID number, set it as an environment variable, and then I retrieve the environment variable value from the later script (the one configured from the .testsetting file). – Ciaran Gallagher Aug 06 '12 at 15:37
  • 1
    That's good. You should write your solution as an answer to this question and accept it. Note that in PowerShell, environment variables (`$env:name`) only exist for the current session, so unless you're saving it off somewhere else, the variable will disappear when your session ends/you close PowerShell. – SpellingD Aug 06 '12 at 15:45
  • I used the .NET Framework to set the variable permanently. Yes, will write it up soon. – Ciaran Gallagher Aug 06 '12 at 17:45

1 Answers1

0
"Microsoft Test Manager : Start automated sanity test"
$testRunID = & "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\TCM.exe" run /create /title:"Automated UI Tests" /planid:27 /suiteid:721 /configid:10 /settingsname:"UI Test Settings" /testenvironment:"MyTestEnvironment" /collection:"http://MyCollection" /teamproject:Main /builddir:"C:\MyBuildDir" /include

"Get test run ID from TCM output"
$testRunID = $testRunID.substring(21)
$testRunID = $testRunID.TrimEnd(".")

"Store test run ID in user environment variable"
[Environment]::SetEnvironmentVariable("CodedUITestRunID", "$testRunID", "User")

This is my solution. I store the output from tcm.exe run /create into the $testRunID, then I remove the start of the string, "Run created with ID: ", then I remove the full stop at the end of the string, and that leaves me with just the test ID number, which I set as an environment variable using .NET code (see here).

Later, I have a scheduled task which assumes the test run has finished, and runs a script which contains (among other things), the following:

"Test Run ID"
$testRunID = [Environment]::GetEnvironmentVariable("CodedUITestRunID", "User")

"Microsoft Test Manager: Export test results"
& "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\TCM.exe" run /export /id:"$testRunID" /resultsfile:"C:\ResultsPath\MyResultsfile.trx" /collection:"http://MyCollection" /teamproject:"Main"

This just retrieves the test run ID from the environment variable I set earlier and then runs the /export command of the Microsoft Test Manager command-line utility (tcm.exe), inputting the test run ID.

Ciaran Gallagher
  • 3,895
  • 9
  • 53
  • 97