'Update build reference for test result in Azure DevOps REST API

I have an existing test result and I'd like to update the "Tested build" field for the test result if possible. I've looked at the REST API doc (https://docs.microsoft.com/en-us/rest/api/azure/devops/test/results/update?view=azure-devops-rest-5.0) and I think that I've called the PATCH method correctly but while a 200 is returned, the test result in the UI doesn't change with "not available".

Is there a way to change this field or is it read-only?

PATCH https://dev.azure.com/{org}/{project}/_apis/test/Runs/{runId}/results?api-version=5.0

Auth: PAT for user who has Test Plans license

JSON request body: 
[
    {
        "id": "100000",
        "build": {
            "id": "206",
            "name": {buildDefinitonName},
            "url": "https://dev.azure.com/{org}/{projectguid}/_apis/build/Builds/206"
        }
    }
]

JSON response body: 
{
    "count": 1,
    "value": [
        {
            "id": 100000,
            "project": {},
            "lastUpdatedDate": "2019-06-13T17:18:56.073Z",
            "priority": 0,
            "url": "",
            "lastUpdatedBy": {
                "displayName": null,
                "id": null
            }
        }
    ]
}

I've also tried with the .NET SDK but no luck:

...
var build = await buildClient.GetBuildAsync(projectGuid, 206);

var testResult = await testClient.GetTestResultByIdAsync(projectGuid, {runId}, 100000);

testResult.Build = new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference
     {
         Id = build.Id.ToString(),
         Name = build.Definition.Name,
         Url = build.Url
     };

TestCaseResult[] results = new TestCaseResult[] { testResult };

var outcome = await testClient.UpdateTestResultsAsync(results, teamProject, {runId});

(outcome shows Build = null)



Solution 1:[1]

I had similar problem, I checked with postman and this API works fine and updates the build into the test result.

Check if you need to set also into your call the build configuration as it is into the image.

On my case It works but it doesn´t work from the SDK not sure why I am still dealing with it.

Postman request

Solution 2:[2]

called the PATCH method correctly but while a 200 is returned, the test result in the UI doesn't change with "not available".

To explain this, first need to know what does the response code 200OK defined:

" It represent a standard response for successful HTTP requests. The actual response will depend on the request method used.

As it say, 200OK just mean that your request URL, body and method are all correct and request successfully. But, for actually put/patch action, it depend on the actual situation. So 200OK does not also mean that the operation is been completed and succeed.

enter image description here

So, for "Tested build" field for the test result in Azure Devops,

Is there a way to change this field or is it read-only?

No, you could not change it with API, even if with UI. It just read only.

You know, after test execution end, the build associated with this test is fixed, this is the actual truth. So, you could not change it, it just read only to tell you what is tested build.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Community