David
David

Reputation: 197

How to successfully post a generic test result to a VSTS build via API?

With the aim of reporting from an external test framework to a VSTS build report - what is the minimum required in a VSTS build definition, to check the ability to post a test result to a test run via the API?

At the moment, I have a minimal proof of concept VSTS build definition containing:

While the above VSTS build is running, I am trying to post a generic 'test result' in with JSON:

 [

 {

    "testCaseTitle": "myTestCaseTitle",

    "automatedTestName": "myAutomatedTestName",

    "priority": 1,

    "outcome": "Passed"

 }

]

To the current test run, at:

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

However it still seems during the build test run, the run is not available on the API to post new test results to until the build completes. And once it completes, it is also not available to post results to, with the server response stating:

TestCaseResults cannot be added or updated for a test run which is in Completed state

So, what is the minimum required in a build definition to open up a valid window with which to post generic test results, if this is even possible? (And can you avoid specifying a particular {run} number, targetting the current run?)

Upvotes: 0

Views: 576

Answers (2)

David
David

Reputation: 197

Quick answer for posting results from an external testing framework into VSTS against a product build created in VSTS:

  1. Define a build process which builds product

  2. Post a new 'test run' against existing build (specify the buildID within the json body)

    { "name": "myTestRun", "build": { "id": "629" }, "isAutomated": true, "state": "Waiting" }

  3. Have your bespoke test controller launched, with it running required tests

  4. As results become available from your test controller, post test results against the run created earlier, eg:

    [ { "testCaseTitle": "myTestCaseTitle", "automatedTestName": "myAutomatedTestName", "priority": 1, "outcome": "Passed" } ]

    1. Patch update the test run to set state as completed:

body:

{
"name": "myTestRun",
"state": "Completed",
"comment":"All tests have been run"

}

Upvotes: 0

starian chen-MSFT
starian chen-MSFT

Reputation: 33708

You can update the test run state first, then add the test result to that test run.

Simple sample steps:

  1. Update test run.

JSON body:

{
    "state":"InProgress"
}
  1. Add test result to test run.

JSON body:

[
    {
        "testCaseTitle":"TestMethod1",
        "automatedTestName": "UnitTestProject1.UnitTest1.TestMethod1",
        "outcome": "Passed"
    }   
]

Regarding thread sleep:

  1. Add PowerShell task to build definition
  2. Select Inline Script

Script:

Start-Sleep -s 3600

Node: if you need to wait a long time (more than 30 minutes), you need to use private agent, about setup a private agent, you can refer to: Deploy an agent on Windows

Upvotes: 1

Related Questions