简体   繁体   English

如何使用Python REST API在Azure DevOps中检索测试结果?

[英]How to retrieve Test Results in Azure DevOps with Python REST API?

How to retrieve Test Results from VSTS (Azure DevOps) by using Python REST API? 如何使用Python REST API从VSTS(Azure DevOps)检索测试结果?

Documentation is (as of today) very light, and even the examples in the dedicated repo of the API examples are light ( https://github.com/Microsoft/azure-devops-python-samples ). (到目前为止)文档非常清淡,甚至API示例专用回购中的示例都很清澈( https://github.com/Microsoft/azure-devops-python-samples )。

For some reasons, the Test Results are not considered as WorkItems so a regular WIQL query would not work. 由于某些原因,测试结果不被视为工作项,因此常规的WIQL查询将不起作用。

Additionally, it would be great to query the results for a given Area Path. 另外,最好查询给定区域路径的结果。

Thanks 谢谢

First you need to get the proper connection client with the client string that matches the test results. 首先,您需要使用与测试结果匹配的客户端字符串来获取正确的连接客户端。

from vsts.vss_connection import VssConnection from msrest.authentication import BasicAuthentication 从vsts.vss_connection导入从msrest.authentication导入VssConnection BasicAuthentication

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

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

TEST_CLIENT = "vsts.test.v4_1.test_client.TestClient"
test_client = connection.get_client(TEST_CLIENT)

Then, you can have a look at all the functions available in: vsts/test/<api_version>/test_client.py" 然后,您可以查看以下所有可用功能: vsts/test/<api_version>/test_client.py"

The following functions look interesting: 以下功能看起来很有趣:

  • def get_test_results(self, project, run_id, details_to_include=None, skip=None, top=None, outcomes=None) (Get Test Results for a run based on filters) def get_test_results(self, project, run_id, details_to_include=None, skip=None, top=None, outcomes=None) (获取基于过滤器的运行的测试结果)
  • def get_test_runs(self, project, build_uri=None, owner=None, tmi_run_id=None, plan_id=None, include_run_details=None, automated=None, skip=None, top=None)
  • def query_test_runs(self, project, min_last_updated_date, max_last_updated_date, state=None, plan_ids=None, is_automated=None, publish_context=None, build_ids=None, build_def_ids=None, branch_name=None, release_ids=None, release_def_ids=None, release_env_ids=None, release_env_def_ids=None, run_title=None, top=None, continuation_token=None) (although this function has a limitation of 7 days range between min_last_updated_date and max_last_updated_date def query_test_runs(self, project, min_last_updated_date, max_last_updated_date, state=None, plan_ids=None, is_automated=None, publish_context=None, build_ids=None, build_def_ids=None, branch_name=None, release_ids=None, release_def_ids=None, release_env_ids=None, release_env_def_ids=None, run_title=None, top=None, continuation_token=None) (尽管此功能在min_last_updated_datemax_last_updated_date之间的限制为7天

To retrieve all the results from the Test Plans in a given Area Path, I have used the following code: 为了从给定的区域路径中的测试计划中检索所有结果,我使用了以下代码:

tp_query = Wiql(query="""
SELECT
        [System.Id]
FROM workitems
WHERE
        [System.WorkItemType] = 'Test Plan'
        AND [Area Path] UNDER 'Development\MySoftware'
ORDER BY [System.ChangedDate] DESC""")

for plan in wit_client.query_by_wiql(tp_query).work_items:
    print(f"Results for {plan.id}")
    for run in test_client.get_test_runs(my_project, plan_id = plan.id):
        for res in test_client.get_test_results(my_project, run.id):
            tc = res.test_case
            print(f"#{run.id}. {tc.name} ({tc.id}) => {res.outcome} by {res.run_by.display_name} in {res.duration_in_ms}")

Note that a test result includes the following attributes: 请注意,测试结果包括以下属性:

  • duration_in_ms
  • build
  • outcome (string) outcome (字符串)
  • associated_bugs
  • run_by (Identity) run_by (身份)
  • test_case (TestCase) test_case (TestCase)
  • test_case_title (string) test_case_title (字符串)
  • area (AreaPath) area (AreaPath)
  • Test_run , corresponding to the test run Test_run ,对应于测试运行
  • test_suite
  • test_plan
  • completed_date (Python datetime object) completed_date (Python日期时间对象)
  • started_date ( Python datetime object) started_date (Python日期时间对象)
  • configuration

Hope it can help others save the number of hours I spent exploring this API. 希望它可以帮助其他人节省我探索此API所花费的时间。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python REST API 在 Azure DevOps 中更新测试结果? - How to Update Test Results in Azure DevOps with Python REST API? 集成了Azure Devops和Python,以便通过自动化脚本传递测试结果并在Azure Devops中进行更新? - Integration of Azure Devops and Python for passing test results from automation script and update in Azure Devops? 如何编写 Python 脚本以对 Azure DevOps REST API 进行身份验证并获取访问令牌? - How to write a python script to authenticate to Azure DevOps REST API and get the access token? 如何使用 Python REST API 从 VSTS (Azure DevOps) 中的查询中提取工作项? - How to extract WorkItems from query in VSTS (Azure DevOps) with Python REST API? Azure DevOps:如何使用 REST API 从 git 下载文件? - Azure DevOps: How to download a file from git using REST API? 如何使用 Azure DevOps 下载 Azure 通用包 - How to download Azure universal packages using Azure DevOps Python API 如何使用 Azure DevOps / VSTS 获取 python 中的查询结果 - How to use Azure DevOps / VSTS to fetch query results in python Azure Devops Rest API 授权 Z099FB995346F31C7549F6E40DB0F3EZ 不工作 - Azure Devops Rest API authorization header not working 使用 rest api 将笔记本部署到 azure devops - deploy notebooks to azure devops using rest api 如何通过 azure-devops-python-api 获取仪表板列表 - How to get a list of dashboards through azure-devops-python-api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM