簡體   English   中英

如何使用Python REST API在Azure DevOps中檢索測試結果?

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

如何使用Python REST API從VSTS(Azure DevOps)檢索測試結果?

(到目前為止)文檔非常清淡,甚至API示例專用回購中的示例都很清澈( https://github.com/Microsoft/azure-devops-python-samples )。

由於某些原因,測試結果不被視為工作項,因此常規的WIQL查詢將不起作用。

另外,最好查詢給定區域路徑的結果。

謝謝

首先,您需要使用與測試結果匹配的客戶端字符串來獲取正確的連接客戶端。

從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)

然后,您可以查看以下所有可用功能: vsts/test/<api_version>/test_client.py"

以下功能看起來很有趣:

  • 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) (盡管此功能在min_last_updated_datemax_last_updated_date之間的限制為7天

為了從給定的區域路徑中的測試計划中檢索所有結果,我使用了以下代碼:

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}")

請注意,測試結果包括以下屬性:

  • duration_in_ms
  • build
  • outcome (字符串)
  • associated_bugs
  • run_by (身份)
  • test_case (TestCase)
  • test_case_title (字符串)
  • area (AreaPath)
  • Test_run ,對應於測試運行
  • test_suite
  • test_plan
  • completed_date (Python日期時間對象)
  • started_date (Python日期時間對象)
  • configuration

希望它可以幫助其他人節省我探索此API所花費的時間。

干杯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM