首页 > 解决方案 > VSTS - 通过 .NET 客户端库或 REST API 以编程方式检索链接到特定测试用例的错误工作项

问题描述

我正在将 VSTS 与 git 一起使用。

我有许多与它们相关联的错误工作项的测试用例。

我想使用 .NET 客户端库或 VSTS REST API 专门检索链接到特定测试用例的所有错误工作项。REST API 的版本可以是 4.0 或更高版本。

我找不到与检索链接到特定测试用例的错误工作项相关的信息,尽管有与检索所有错误工作项相关的信息。

这是我尝试过的代码:

静态列表 GetLinkedWorkItems() { int[] workitemIds = new int[] { 12697 };

        //VssConnection connection = Context.Connection;
        VssConnection connection = new VssConnection(new Uri(vstsCollectionUrl), new VssClientCredentials());
        WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();

        List<Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.WorkItem> workitems = workItemTrackingClient.GetWorkItemsAsync(workitemIds, expand: WorkItemExpand.Links | WorkItemExpand.Relations).Result;

        foreach (var workitem in workitems)
        {
            Console.WriteLine("Work item {0}", workitem.Id);

            if (workitem.Links != null)
            {
                foreach (var link in workitem.Links.Links)
                {
                    Console.WriteLine("  {0} {1}", link.Key, link.Value);
                }
            }
        }


        return workitems;
    }

请注意,VSTS 没有连接问题。另外,我尝试使用下面给出的基于查询的方法,但没有用:

        VssConnection connection = new VssConnection(new Uri(vstsCollectionUrl), new VssClientCredentials());

        //create http client and query for resutls
        WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();

        Wiql query = new Wiql() { Query = "SELECT [Id], [Title], [State] FROM workitems WHERE [Work Item Type] = 'Test Case'" };
        WorkItemQueryResult queryResults = witClient.QueryByWiqlAsync(query).Result;

        //Display reults in console
        var l = queryResults.WorkItemRelations;
        var t = queryResults.WorkItems.Skip(0).Take(100);
        if (queryResults == null || queryResults.WorkItems.Count() == 0)
        {
            Console.WriteLine("Query did not find any results");
        }
        else
        {
            foreach (var item in queryResults.WorkItems)
            {
                Console.WriteLine(item.Id);
                Console.WriteLine(item.Url);
            }
        }

标签: azure-devopsazure-devops-rest-api

解决方案


因此,您需要做的是通过删除它与 bug 工作项的关系来更新测试用例。您可以通过更新工作项 REST API来实现它。详情如下:

1. 获取测试用例的关系工作项

使用获取工作项 REST API

GET https://account.visualstudio.com/project/_apis/wit/workitems/workitemID?$expand=all&api-version=4.1

2.获取相关bug工作项的计数

从 step1 的响应中获取相关工作项的计数。如果链接的 WIT 是 bug,那么关系的计数就是链接的 bug 的计数。假设只有三个错误与测试用例相关联。

3. 更新测试用例,去除相关Bug

删除相关工作项的路径,/relations/index索引是基于以 开头的链接工作项的计数0。因此,如果只有三个错误链接到测试用例,您可以删除带有/relations/0/relations/1和的路径/relations/2

更新工作项 REST API 如下:

PATCH https://account.visualstudio.com/project/_apis/wit/workitems/workitemID?api-version=4.1

应用程序/json-补丁+json:

[

  {
    "op": "remove",
    "path": "/relations/0"
  },
  {
    "op": "remove",
    "path": "/relations/1"
  }
  {
    "op": "remove",
    "path": "/relations/2"
  }

]

推荐阅读