如何通过Azure DevOps API获取所有测试用例步骤数据?
Absolutely! You can definitely fetch all test case steps using the Azure DevOps REST API. Let me break down the process clearly, with actionable examples:
1. Choose the Right API Endpoint
Test cases are treated as work items in Azure DevOps, so you have two main options:
- Single test case: Use the
Test Cases - Get Test Caseendpoint to pull details for one specific test case. - Multiple test cases: Use the
Work Items - Listendpoint (or query via WIQL) to retrieve steps for a batch of test cases.
2. Example: Fetch a Single Test Case's Steps
Send a GET request to this endpoint (replace placeholders with your org/project/test case ID):
GET https://dev.azure.com/{organization}/{project}/_apis/test/testcases/{testCaseId}?api-version=7.1-preview.3
In the JSON response, look for the Microsoft.VSTS.TCM.Steps field under workItem.fields. This field stores all test steps as an XML string. Here's a snippet of what that might look like:
"fields": { "Microsoft.VSTS.TCM.Steps": "<steps><step id=\"1\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\">Action: Navigate to login page</parameterizedString><parameterizedString isformatted=\"true\">Expected: Login form is displayed</parameterizedString></step><step id=\"2\" type=\"ValidateStep\">...</step></steps>" }
3. Example: Fetch Steps for Multiple Test Cases
To get steps for multiple test cases at once, use the Work Items List endpoint with a comma-separated list of IDs and specify the Microsoft.VSTS.TCM.Steps field:
GET https://dev.azure.com/{organization}/{project}/_apis/wit/workitems?ids=123,456,789&fields=Microsoft.VSTS.TCM.Steps&api-version=7.1-preview.3
If you need to filter test cases by criteria like area path or iteration, use the Queries - Query By Wiql endpoint to run a WIQL query (e.g., SELECT [System.Id], [Microsoft.VSTS.TCM.Steps] FROM WorkItems WHERE [System.WorkItemType] = 'Test Case'), then fetch the work items from the query results.
4. Key Tips
- Permissions: Ensure you have Read access to the test cases in your project—without this, the API will return a 403 error.
- Parsing XML: Since steps are stored as XML, you’ll need to parse this string in your code (using an XML parser library) to extract individual step actions and expected results.
- API Versions: Stick to the latest stable preview version (check Azure DevOps documentation for updates) to avoid hitting deprecated endpoints.
内容的提问来源于stack exchange,提问作者Andrey Kudin

