Skip to content

Instantly share code, notes, and snippets.

@jstangroome
Last active December 20, 2015 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstangroome/6075788 to your computer and use it in GitHub Desktop.
Save jstangroome/6075788 to your computer and use it in GitHub Desktop.
Bulk rename a test assembly, class, and/or namespace referenced by test case automation
#requires -version 3.0
function Rename-TestCaseAutomation {
[CmdletBinding()]
param (
[Parameter(Mandatory, Position=0)]
[string]
$CollectionUri,
[Parameter(Mandatory, Position=1)]
[string[]]
$TeamProject,
[Parameter(ParameterSetName='RenameTestStorage', Mandatory)]
[Parameter(ParameterSetName='RenameTestName')]
[Parameter(ParameterSetName='RenameTestBoth')]
[string]
$TestStorage,
[Parameter(ParameterSetName='RenameTestStorage')]
[Parameter(ParameterSetName='RenameTestName', Mandatory)]
[Parameter(ParameterSetName='RenameTestBoth', Mandatory)]
[string]
$TestNamePrefix,
[Parameter(ParameterSetName='RenameTestStorage', Mandatory)]
[Parameter(ParameterSetName='RenameTestBoth', Mandatory)]
[string]
$NewTestStorage,
[Parameter(ParameterSetName='RenameTestName', Mandatory)]
[Parameter(ParameterSetName='RenameTestBoth', Mandatory)]
[string]
$NewTestNamePrefix
)
#Set-StrictMode -Version Latest
Add-Type -AssemblyName 'Microsoft.TeamFoundation.Client, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
Add-Type -AssemblyName 'Microsoft.TeamFoundation.TestManagement.Client, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
$Collection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($CollectionUri)
$TestManagement = $Collection.GetService('Microsoft.TeamFoundation.TestManagement.Client.ITestManagementService')
$TestCaseQuery = 'select * from WorkItem '
if ($TestStorage -and $TestNamePrefix) {
$TestCaseQuery += 'where Microsoft.VSTS.TCM.AutomatedTestStorage = @TestStorage and Microsoft.VSTS.TCM.AutomatedTestName contains @TestNamePrefix'
} elseif ($TestStorage) {
$TestCaseQuery += 'where Microsoft.VSTS.TCM.AutomatedTestStorage = @TestStorage'
} else {
$TestCaseQuery += 'where Microsoft.VSTS.TCM.AutomatedTestName contains @TestNamePrefix'
}
$Provider = New-Object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider
$TeamProject |
ForEach-Object {
$TestProject = $TestManagement.GetTeamProject($_)
$TestProject.TestCases.Query($TestCaseQuery, @{ TestStorage = $TestStorage ; TestNamePrefix = $TestNamePrefix })
} |
ForEach-Object {
$Case = $_
$Impl = $Case.Implementation
if (-not $TestNamePrefix -or $Impl.TestName -like "$TestNamePrefix*") {
$TestName = $Impl.TestName
if ($NewTestNamePrefix) {
$TestName = $NewTestNamePrefix + $Impl.TestName.Substring($TestNamePrefix.Length)
}
$Storage = $Impl.Storage
if ($NewTestStorage) {
$Storage = $NewTestStorage
}
$NewTestId = $Impl.TestId
if ($TestName -cne $Impl.TestName) {
# TestId regenerated to support tcm.exe testcase /import. Implementation from:
# http://blogs.msdn.com/b/gautamg/archive/2012/01/01/how-to-associate-automation-programmatically.aspx
$UnicodeBytes = [System.Text.Encoding]::Unicode.GetBytes($TestName)
$HashBytes = [byte[]]$Provider.ComputeHash($UnicodeBytes)[0..15]
$NewTestId = New-Object -TypeName System.Guid -ArgumentList (,$HashBytes)
}
$NewImpl = $Case.Project.CreateTmiTestImplementation( $TestName, $Impl.TestType, $Storage, $NewTestId )
$Case.WorkItem.Open()
$Case.Implementation = $NewImpl
$Case.Save()
$Case
}
} |
Select-Object -Property Id, Title,
@{n='TestName';e={$_.Implementation.TestName}},
@{n='Storage';e={$_.Implementation.Storage}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment