如何通过TestCase ID或WIQL查询获取VSTS的测试运行结果?
Absolutely! You’ve got two reliable ways to pull test run outcomes by Test Case ID in Azure DevOps (formerly VSTS)—either via direct REST API calls or using WIQL (Work Item Query Language). Let’s walk through both:
1. Use the REST API Test Results Endpoint
You can use the Test Results - List endpoint to filter results specifically by your Test Case ID, no need to know individual run IDs upfront. Here’s how:
- The request URL (replace placeholders with your actual values):
GET https://dev.azure.com/{organization}/{project}/_apis/test/results?testCaseId={your-test-case-id}&api-version=7.1-preview.6 - Key details:
testCaseId: Replace with the ID of the test case you’re targetingapi-version: Use a recent version (you can check Azure DevOps documentation for the latest stable option if preview builds aren’t ideal for your workflow)
- Response output: The returned data includes all test results for that test case, with critical fields like
outcome(Passed, Failed, Skipped, etc.), associated test run ID, test run title, and completion timestamps.
2. Use WIQL (Work Item Query Language)
WIQL lets you query test result entities directly, which is a flexible approach if you prefer a query-based workflow. Here’s how to implement it:
- Craft a WIQL query to filter test results by your target Test Case ID:
SELECT ResultId, TestCaseId, Outcome, RunId, Title, CompletedDate FROM TestResult WHERE TestCaseId = {your-test-case-id} - Execute the query via the
Queries - Query By WiqlAPI:- Send a POST request to this URL:
POST https://dev.azure.com/{organization}/{project}/_apis/wit/wiql?api-version=7.1-preview.2 - Include this JSON request body (replace the query with your own):
{ "query": "SELECT ResultId, TestCaseId, Outcome, RunId, Title FROM TestResult WHERE TestCaseId = 12345" }
- Send a POST request to this URL:
- The response will return a list of matching test results, including the
Outcomevalue you need. You can expand the query to pull additional fields likeTesterorRunTitleif required.
Quick Extra Tips
- Permissions: Ensure your service account or personal access token (PAT) has the
View test runspermission enabled for the project. - Full run details: If you need more context about the test run itself (like run owner or configuration), take the
RunIdfrom your results and call theTest Runs - Getendpoint:GET https://dev.azure.com/{organization}/{project}/_apis/test/Runs/{runId}?api-version=7.1-preview.3
内容的提问来源于stack exchange,提问作者Hansmukh Jain
相关产品推荐
相关产品推荐

