如何提取查询工作项在VSTS(Azure中的DevOps)与Python REST API?(How

2019-10-30 03:48发布

我使用Azure中的DevOps的官方Python REST API: https://github.com/Microsoft/azure-devops-python-api

由于样品我已经能够从IDS获取测试用例的信息。

如何从查询(WIQL)做呢?

这是到目前为止我的代码(改性令牌和链接):

from vsts.vss_connection import VssConnection
from msrest.authentication import BasicAuthentication


token = "hcykwckuhe6vbnigsjs7r3ai2jefsdlkfjslkfj5mxizbtfu6k53j4ia"
team_instance = "https://tfstest.toto.com:8443/tfs/Development/"

credentials = BasicAuthentication("", token)
connection = VssConnection(base_url=team_instance, creds=credentials)


def print_work_items(work_items):
    for work_item in work_items:
        print(
            "{0} {1}: {2}".format(
                work_item.fields["System.WorkItemType"],
                work_item.id,
                work_item.fields["System.Title"],
            )
        )


WIT_CLIENT = (
    "vsts.work_item_tracking.v4_1.work_item_tracking_client.WorkItemTrackingClient"
)
wit_client = connection.get_client(WIT_CLIENT)


def get_TC_by_id(desired_ids):
    work_items = wit_client.get_work_items(ids=desired_ids, error_policy="omit")
    print_work_items(work_items)


def get_TC_from_query(query):
    # THIS FUNCTION IS NOT WORKING...
    work_items = wit_client.get_work_items(query=query, error_policy="omit")
    print_work_items(work_items)


get_TC_by_id([1035375])

get_TC_from_query(
    """\
SELECT
        [System.Id],
        [System.WorkItemType],
        [System.Title],
        [System.State],
        [System.AreaPath],
        [System.IterationPath]
FROM workitems
WHERE
        [System.TeamProject] = @project
        AND [System.WorkItemType] = 'Test Case'
ORDER BY [System.ChangedDate] DESC
"""
)

这是我的错误

  File "test_TFS.py", line 35, in get_TC_from_query
    work_items = wit_client.get_work_items(query=query, error_policy="omit")
TypeError: get_work_items() got an unexpected keyword argument 'query'

我应如何检索查询测试案例?

特别是,我不明白“客户”的价值观,如"vsts.work_item_tracking.v4_1.work_item_tracking_client.WorkItemTrackingClient"

谢谢你们!

Answer 1:

发布的消息(即问题导致后拉请求 )在Github上存储库VSTS的例子,我有一个提示,解决我的问题。

该解决方案是使用:

  • 用wiql查询对象Wiql
  • query_by_wiql功能
  • 变换所述查询(与工作项目ID引用)的结果与对WORKITEM get_work_item函数(或get_work_items到在单次通过处理多个工作项)

这是我解决我的问题:

from vsts.vss_connection import VssConnection
from msrest.authentication import BasicAuthentication
from vsts.work_item_tracking.v4_1.models.wiql import Wiql


token = "hcykwckuhe6vbnigsjs7r3ai2jefsdlkfjslkfj5mxizbtfu6k53j4ia"
team_instance = "https://tfstest.toto.com:8443/tfs/Development/"

credentials = BasicAuthentication("", token)
connection = VssConnection(base_url=team_instance, creds=credentials)


def print_work_items(work_items):
    for work_item in work_items:
        print(
            "{0} {1}: {2}".format(
                work_item.fields["System.WorkItemType"],
                work_item.id,
                work_item.fields["System.Title"],
            )
        )


WIT_CLIENT = (
    "vsts.work_item_tracking.v4_1.work_item_tracking_client.WorkItemTrackingClient"
)
wit_client = connection.get_client(WIT_CLIENT)


def get_TC_from_query(query):
    query_wiql = Wiql(query=query)
    results = wit_client.query_by_wiql(query_wiql).work_items
    # WIQL query gives a WorkItemReference => we get the corresponding WorkItem from id
    work_items = (wit_client.get_work_item(int(result.id)) for result in results)
    print_work_items(work_items)


文章来源: How to extract WorkItems from query in VSTS (Azure DevOps) with Python REST API?