Jenkins: Is there any API to see test reports remotely?

13,243

Solution 1

Use the XML or Json API. At most pages on Jenkins you can add /api/ to the url and get data in xml, json and similar formats. So for a job you can go to <Jenkins URL>/job/<Job Name>/api/xml and get informaiton about the job, builds, etc. For a build you can go to <Jenkins URL>/job/<Job Name>/<build number>/api/xml and you will get a summary for the build. Note that you can use the latestXXXBuild in order to get the latest successful, stable, failing, complete build, like this; <Jenkins URL>/job/<Job Name>/lastCompletedBuild/api/xml.

Additionally if youre using any plugin which publishes test results to the build, then for a given job you can go to <Jenkins URL>/job/<Job Name>/lastCompletedBuild/testReport/api/xml and you will get an xml report with results.

There is a lot more to it, you can control what is exported with the tree parameter and depth parameter. For a summary go to <Jenkins URL>/api/

Solution 2

Well, if you are using a jenkins shared library or decided to permit the security exceptions (a less good approach) then you can access them via a job and send them out to whatever you like - push vs pull

def getCurrentBuildFailedTests() {
    def failedTests = []
    def build = currentBuild.build()

    def action = build.getActions(hudson.tasks.junit.TestResultAction.class)
    if (action) {
        def failures = build.getAction(hudson.tasks.junit.TestResultAction.class).getFailedTests()
        println "${failures.size()} Test Results Found"
        for (def failure in failures) {
            failedTests.add(['name': failure.name, 'url': failure.url, 'details': failure.errorDetails])
        }
    }

    return failedTests
}
Share:
13,243
Mojtaba Yousefi
Author by

Mojtaba Yousefi

Updated on June 09, 2022

Comments

  • Mojtaba Yousefi
    Mojtaba Yousefi almost 2 years

    I'm using jenkins as CI tool. I used restful api to build a job remotely but I don't know how to get test result remotely as well. I can't be more thankful if anybody know a solution