Search code examples
azurejunitazure-devopsazure-pipelinespytest

How to Link Azure Pipeline Test Results to Test Cases


I’m trying to link automated test results from a pipeline task PublishTestResults, to test cases in azure. That way I am able view the history of the test results through the test case

Background

These tests are automated with pytest and generate a Junit XML report to Azure. Please correct me if I am wrong but from what I found, there isn’t an argument or option or setting where azure is able to link these test cases automatically. Also the XML format Azure expects doesn’t contain a attribute for the id. https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/test/publish-test-results?view=azure-devops&tabs=trx%2Cyaml

I can’t associate these tests through visual studio since pytest is not supported. https://learn.microsoft.com/en-us/azure/devops/test/associate-automated-test-with-test-case?view=azure-devops

Problem

I started using the Azure REST API and the azure devops python api, https://github.com/microsoft/azure-devops-python-api . Got my build id, test run id associated to the pipeline build, test run from the id, test case id, and the test point id. Doing a GET Test Result returns the test result, and focusing on a particular object of the response.

"testCase": {
    "name": "test title"
},

However when I do a PATCH Test Result to update the test result with

"testCase": {
    "id": "5688",
    "name": "test title"
},
"testPoint": {
    "id": "296"
},

It does not update, and remains the same. I can confirm that I am able to update other fields, and I see the lastUpdatedDate updates as well. Referring to this call. https://learn.microsoft.com/en-us/rest/api/azure/devops/test/results/update?view=azure-devops-rest-6.0

I tried a manual test run, and was able to add test results with test case id's. However I couldn't edit the test case id's after. I tried adding a test result with test case id's to my automated test runs but that did not work

Anyone have any ideas what I am doing wrong or a better way to go about linking test results to test cases in Azure?


Solution

  • I posted to Visual Studio Developer Community Forum.

    We pretty much have to go through the "Update Work Item" route. In order to link the automated test results with a test case, I added a relation link to the test result:

    [
        {
            "op": "add",
            "path": "/relations/-",
            "value": {
                "rel": "ArtifactLink",
                "url": "vstfs:///TestManagement/TcmResult/1234.100000",
                "attributes": {
                    "name": "Test Result"
                }
            }
        }
    ]
    

    This will add a link from the test case to direct you to an invalid URL, so it just puts you on the Build Summary page. Not great since it's missing /results in the URL:

    https://dev.azure.com/{organization}/{project}/_build?buildId=1122&view=ms.vss-test-web.build-test-results-tab&runId=1234&resultId=100000&paneView=debug
    

    But outside of that, this works.