简体   繁体   中英

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?

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

For some reasons, the Test Results are not considered as WorkItems so a regular WIQL query would not work.

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

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"

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_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

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)
  • associated_bugs
  • run_by (Identity)
  • test_case (TestCase)
  • test_case_title (string)
  • area (AreaPath)
  • Test_run , corresponding to the test run
  • test_suite
  • test_plan
  • completed_date (Python datetime object)
  • started_date ( Python datetime object)
  • configuration

Hope it can help others save the number of hours I spent exploring this API.

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM