Search code examples
visual-studio-2010automated-testsmicrosoft-test-managertcm

Getting the test run ID after launching a test via tcm


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?


Solution

  • "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.