如何使用Python REST API从VSTS(Azure DevOps)中的查询中提取工作项?

发布于 2021-01-29 14:59:11

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

多亏了这些示例,我才能够从id检索有关测试用例的信息。

如何通过查询(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"

谢谢你们!

关注者
0
被浏览
63
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    在VSTS示例的Github存储库中发布了一条消息(即导致请求请求的问题)之后,我得到了解决问题的提示。

    解决方案是使用:

    • 一个带有Wiql类的wiql查询对象
    • query_by_wiql 功能
    • 将查询结果(具有WorkItem ID的引用)转换为具有get_work_item功能的WorkItem (或get_work_items通过一次处理处理多个WorkItem)

    这是我对这个问题的解决方案:

    [针对版本5.1和azure-devops模块进行了更新,以取代VSTS]

    from azure.devops.connection import Connection
    from msrest.authentication import BasicAuthentication
    from azure.devops.v5_1.work_item_tracking.models import Wiql
    
    
    token = "hcykwckuhe6vbnigsjs7r3ai2jefsdlkfjslkfj5mxizbtfu6k53j4ia"
    team_instance = "https://tfstest.toto.com:8443/tfs/Development/"
    
    credentials = BasicAuthentication("", token)
    connection = Connection(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 = connection.clients.get_work_item_tracking_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)
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看