I need to get a list of test cases from a request to TFS.

TfsTeamProjectCollection tfsTeamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("server_address")); ITestManagementService testMangementService = (ITestManagementService)tfsTeamProjectCollection.GetService(typeof(ITestManagementService)); ITestManagementTeamProject testManagementTeamProject = testMangementService.GetTeamProject("project_name"); 

Next, I get a list of all requests

 IList<ITestCaseQuery> testCaseQuery = testManagementTeamProject.Queries; 

Then I get a list of test cases in the request

 List<ITestCase> testCaseList = testManagement.TestCases.Query(testCaseQuery["selected_query"].QueryText).ToList(); 

The problem is that if the Type of Query is Flat list of work items, then I get all the test cases in the query, and if the Type of Query is Work items and direct link or Tree of work items, it throws an exception: "TF248021: You can’t give you a list of work items. If the query type is "Flat list of work items", then the query string will be: "select [System.Id], ... from WorkItems ...", and if - "Work items and direct link" or "Tree of work items ", then this:" select [System.Id], ... from WorkItemLinks ... "This happens if I add a Product Backlog Item to the query, in addition to test cases.

About the value of the testCaseQuery field ["selected_query"]. QueryText. If the field value is:

"select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.Tags] from WorkItems where [System.TeamProject] = @ project and ( [System.WorkItemType] = 'Test Case' and [System.Title] contains words 'new' or [System.WorkItemType] = 'Product Backlog Item') "then everything works. And if the value is:

"select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.Tags] from WorkItemLinks where (Source. [System.TeamProject] = @ project and (Source. [System.WorkItemType] = 'Test Case' or Source. [System.WorkItemType] = 'Task' or Source. [System.WorkItemType] = 'Feature')) and ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') and (Target. [System.TeamProject] = @ project and Target. [System.WorkItemType] <> ") mode (Recursive)" "

then throws an exception.

  • so what's the problem? - tym32167 pm
  • The problem is that I can get a list of test cases only if the request type is "Flat list of work items", and I need it for other types of request. Those. If the request type is, for example, Work items and direct link and there are test cases in this request, then I will get an exception. - ivan pupkin 2:53 pm
  • You write that you get an exception, but do not give any information about this exception, nor the exception message, nor the stack, nothing. And the fact of having an exception without a description is useless. - tym32167 2:58 pm
  • here rest api to TFS, here client , you used it? Or what? What is the version of TFS? - tym32167
  • I’m getting the exception: "If you have a list of work items. specified. " If the query type is "Flat list of work items", then the query string will be: "select [System.Id], ... from WorkItems ...", and if - "Work items and direct link" or "Tree of work items ", then this:" select [System.Id], ... from WorkItemLinks ... "This happens if I add to the query, in addition to test cases, a Product Backlog Item - ivan pupkin

1 answer 1

In fact, it is not very clear why the selection of test cases is performed through the testManagement client. In order to select items in any form, the WorkItem Client is used. The result of the execution of the flat list and the result with the links is different. A simple list contains a list of items, and a tree or links contains a link structure in the form Source-> Target. To build a correct query, I recommend that in VS, first create a query with the necessary conditions, and save it not in tfs, but on a local disk. There you will see a properly constructed wiql request.

Examples of running queries:

  1. Simple list:

     private static void GetTasksWithWIQL(WorkItemStore pWiStore) { List<WorkItem> _parents = new List<WorkItem>(); string _wiql = String.Format("SELECT [System.Id] FROM WorkItems WHERE ([System.WorkItemType] = 'Task') ORDER BY [System.Id]"); Query _query = new Query(pWiStore, _wiql); WorkItemCollection _wis = _query.RunQuery(); for (int i = 0; i < _wis.Count; i++) Console.WriteLine("Work item: {0}:{1}:{2}", _wis[i].Type.Name, _wis[i].Id, _wis[i].Title); } 
  2. List with links:

     private static void GetParentsWithWIQL(WorkItemStore pWiStore) { string _wiql = String.Format("SELECT [System.Id] FROM WorkItemLinks WHERE ([Source].[System.WorkItemType] = 'User Story') And ([Target].[System.WorkItemType] = 'Task') And ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') ORDER BY [System.Id] mode(Recursive,ReturnMatchingChildren)"); Query _query = new Query(pWiStore, _wiql); WorkItemLinkInfo[] _links = _query.RunLinkQuery(); for (int i = 0; i < _links.Count(); i++) if (_links[i].SourceId == 0) Console.WriteLine("This is top level {1}", _links[i].SourceId, _links[i].TargetId); else Console.WriteLine("Parent - {0}; Child - {1}", _links[i].SourceId, _links[i].TargetId); } 
  3. Getting a test through a work item:

     private static void GetTests(WorkItemStore pWiStore, string pTeamProjectName) { ITestManagementTeamProject _testProject = pWiStore.TeamProjectCollection.GetService<ITestManagementService>().GetTeamProject(pTeamProjectName); string _wiql = String.Format("SELECT [System.Id] FROM WorkItems WHERE [System.WorkItemType] = 'Test Case' And [System.TeamProject] = '{0}' ORDER BY [System.Id]", pTeamProjectName); Query _query = new Query(pWiStore, _wiql); var _wis = _query.RunQuery(); for (int i = 0; i < _wis.Count; i++) if (_wis[i].Type.Name == "Test Case") { var _testCaseToEdit = _testProject.TestCases.Find(_wis[i].Id); //edit test case here } } 
  • How to get from the WorkItem with the type Test Case the Actions contained in this test case? - ivan pupkin
  • @ivanpupkin Using the TestCases.Find (ID) method, you can get a test. The answer is updated. - Shamrai Aleksander
  • It turned out, thank you all) - ivan pupkin