Post

Automating Test Cases Deletion in Azure DevOps with PowerShell

While working on a particular use case, I encountered a requirement to delete all Test Cases within my Azure DevOps project. Unfortunately, the user interface (UI) does not provide an option to accomplish this task. Upon exploring the available API options, I discovered that there is no direct API for deleting multiple Test Cases at once; only one Test Case can be deleted at a time.

To address this limitation, I developed a PowerShell script to streamline the process. The script is designed to retrieve Test Cases based on a specific query, iterate through them, and invoke the deletion API for each Test Case. This approach ensures an automated and efficient process for mass deletion, overcoming the absence of a built-in feature in the UI.

Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Variables (Update these as needed)
$organizationUrl = "ORGANIZATION_URL"
$projectName = "PROJECT_NAME"
$pat = "PAT"

function New-AuthorizationHeader {
    param (
        [string]$Token
    )

    $headerValue = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($Token)"))
    return @{ Authorization = "Basic $headerValue" }
}

function Get-AzureDevOpsTestCases {
    param (
        [string]$OrganizationUrl,
        [string]$ProjectName,
        [string]$Pat
    )

    $header = New-AuthorizationHeader -Token $Pat

    $query = "Select [System.Id] From WorkItems Where [System.WorkItemType] = 'Test Case' and [System.TeamProject] = '$ProjectName'"
    $body = @{ query = $query } | ConvertTo-Json
    $url = "$OrganizationUrl/$ProjectName/_apis/wit/wiql?api-version=7.1"

    try {
        $response = Invoke-RestMethod -Uri $url -Method POST -ContentType "application/json" -Headers $header -Body $body
        return $response.workItems
    } catch {
        $Host.UI.WriteErrorLine("Error retrieving test cases: $_")
    }
}

function Delete-AzureDevOpsTestCase {
    param (
        [string]$OrganizationUrl,
        [string]$ProjectName,
        [string]$Pat,
        [string]$TestCaseId
    )

    $header = New-AuthorizationHeader -Token $Pat
    $uri = "$OrganizationUrl/$ProjectName/_apis/test/testcases/$TestCaseId/?api-version=7.1"

    try {
        Write-Host "Deleting Test Case $TestCaseId"
        Invoke-RestMethod -Uri $uri -Method DELETE -ContentType "application/json" -Headers $header
        Write-Host "Deleted Test Case: $TestCaseId"
    } catch {
        $Host.UI.WriteErrorLine("Error deleting test case ${TestCaseId}: $_")
    }
}


function Remove-AllAzureDevOpsTestCases {
    param (
        [string]$OrganizationUrl,
        [string]$ProjectName,
        [string]$Pat
    )

    $testCases = Get-AzureDevOpsTestCases -OrganizationUrl $OrganizationUrl -ProjectName $ProjectName -Pat $Pat

    foreach ($testCase in $testCases) {
        $testCaseId = $testCase.id
        Delete-AzureDevOpsTestCase -OrganizationUrl $OrganizationUrl -ProjectName $ProjectName -Pat $Pat -TestCaseId $testCaseId
    }

    Write-Host "All test cases deleted."
}

Remove all test cases

Remove-AllAzureDevOpsTestCases -OrganizationUrl $organizationUrl -ProjectName $projectName -Pat $pat

Dive into the script

1. Variable Initialization:

  • $organizationUrl: The URL of the Azure DevOps organization.
  • $projectName: The name of the Azure DevOps project where the test cases are located.
  • $pat: Personal Access Token (PAT) used for authentication and authorization.

2. Functions:

  • New-AuthorizationHeader: This function takes a token as a parameter and returns an authorization header. It uses Basic authentication with the provided token.

  • Get-AzureDevOpsTestCases: This function retrieves test cases from Azure DevOps based on the specified organization URL, project name, and PAT. It uses a WIQL (Work Item Query Language) query to select test cases and returns the list of work items. You can modify the $query variable to meet your specific filtering requirements, ensuring the returned list aligns precisely with your needs.

  • Delete-AzureDevOpsTestCase: This function deletes a specific test case identified by its ID. It takes the organization URL, project name, PAT, and the ID of the test case to be deleted as parameters.

  • Remove-AllAzureDevOpsTestCases: This function retrieves all test cases using Get-AzureDevOpsTestCases and then iterates through each one, deleting it using Delete-AzureDevOpsTestCase. Finally, it prints a message indicating that all test cases have been deleted.

3. Execution:

The last section of the script calls Remove-AllAzureDevOpsTestCases with the specified organization URL, project name, and PAT. This triggers the removal of all test cases in the specified Azure DevOps project.

  1. Ensure that you have the necessary permissions in Azure DevOps to query and delete test cases.
  1. Make sure to replace the placeholder values for $organizationUrl, $projectName, and $pat with your actual Azure DevOps organization URL, project name, and PAT before running the script.
  1. Always be cautious when running scripts, especially those that modify or delete data. Test the script in a safe environment before using it in a production setting.

References

Wiql - Query By Wiql

Test Cases - Delete

Delete test artifacts in Azure Boards

This post is licensed under CC BY 4.0 by the author.