Search code examples
seleniumselenium-webdriverazure-devopsmicrosoft-test-manager

How to update test results in VSTS/Microsoft Test Manager using APIs/endpoint


How to update the test results in VSTS/Microsoft Test Manager using APIs. How to update the test results in VSTS/Microsoft Test Manager using Selenium/Java.


Solution

  • Procedure to update the test results through APIs:

    1. First get the testPoints of the test case using following API: For this you need plan id, suite id, and test case id. Open the suite in web view, in URL you can find plan id and suite id, test case id is id of the test case.

      GET https://{instance}/DefaultCollection/{project}/_apis/test/plans/{plan}/suites/{suite}/points?api-version={version}&testCaseId={string}

    Send the request and note the the testPoints (in response).

    For details refer: https://learn.microsoft.com/en-us/vsts/integrate/previous-apis/test/points?view=vsts#get-a-test-point

    1. Then create a test run for this. To create a test run you need plan id, testPoints, and run name. You already have plan id, You got testPoints from previous request, run name can be anything. Sample request:

    POST https://{instance}/DefaultCollection/{project}/_apis/test/runs?api-version={version}

    Sample body

    {
      "name": "Sprint 10 Test Run",
      "plan": {
        "id": 1234
      },
      "pointIds": [
        10
      ]
    }
    

    Send the request and note the run id (response).

    For details refer: https://learn.microsoft.com/en-us/vsts/integrate/previous-apis/test/runs?view=vsts#create-new-test-run

    1. Add test results. For this you need test run id (which you got from previous request), test case id, and test points (you got it from first request).

    POST https://{instance}/DefaultCollection/{project}/_apis/test/runs/{run}/results?api-version={version}

    Sample body

     [
          {
            "state": "Completed",
            "testPoint": {
              "id": 10
            },
            "outcome": "Passed",
            "testCase": {
              "id": 4567
            }
          }
        ]
    

    Send the request and note result id (from response).

    For details refer: https://learn.microsoft.com/en-us/vsts/integrate/previous-apis/test/results?view=vsts#add-test-results-to-a-test-run

    1. Update test run For this you need results id from previous request (add result)

    PATCH

    https://{instance}/DefaultCollection/{project}/_apis/test/runs/{run}?api-version={version}
    

    Sample body

    [
      {
        "id": 100000,
        "state": "Completed"
      }
    ]
    

    For details refer: https://learn.microsoft.com/en-us/vsts/integrate/previous-apis/test/runs?view=vsts#update-test-run

    Now check VSTS/Test Manager for result updates. Additionally you can update the results for specific configuration also, just add configuration also in the body of add test results. For configuration details refer: https://learn.microsoft.com/en-us/vsts/integrate/previous-apis/test/configurations?view=vsts#get-a-list-of-test-configurations

    Now to update the results using Java, use RestAssured to send get, post, patch request and retrieve the specific data from the response. For rest-assured details refer: https://github.com/rest-assured/rest-assured/wiki/Usage

    For sending post and patch request you may want to create json objects/bodies, for that use minidev json libraries. How to create json object/array: How to create JSON Object using String?