Search code examples
jenkinspostmannewman

How to combine newman collection test results into one valid xml file for Jenkins?


I've got a small Jenkins pipeline which tests different Postman collections sequentially and after that I combine the single XML files into one to pass them to Jenkins as a result.

Pipeline snippet:

...
steps {
  script {
    try {
        sh '(cd ./integrativeTests/collections && rm -rf *.xml)'
        sh '(cd ./integrativeTests/collections && npm run tests-all)'

        sh '''
        cd ./integrativeTests/collections

        echo '<?xml version="1.0" encoding="UTF-8"?>' > newman_dev_results.xml
        echo '<testsuites>' >> newman_dev_results.xml

        for f in COLLECTION-*.xml
        do
          echo $(sed 1,$(expr $(grep -m1 -n "<testsuite" ${f} | cut -f1 -d:) - 1)d ${f}) >> newman_dev_results.xml
        done

        echo '</testsuites>' >> newman_dev_results.xml
        cat newman_dev_results.xml
        '''

        sh '(cp ./integrativeTests/collections/newman_dev_results.xml ./newman_results.xml)'
        currentBuild.result = 'SUCCESS'
    } catch(Exception e) {
        currentBuild.result = 'FAILURE'
    }
    junit 'newman_results.xml'
  }
}
...

The resulting XML looks like this:

xml screenshot

But sadly I get an ERROR in the Jenkins log:

ERROR: None of the test reports contained any result
Finished: FAILURE

What's the correct xml-layout for a test result with multiple collections for Jenkins or how do I pass multiple test results to Jenkins?


Solution

  • As found in the official docs of the Junit Plugin I don't need to combine all xml by myself and pass a single file. I just need to pass all XMLs at once with a wildcard.

    Pipeline:

    ...
    steps {
        script {
            try {
                sh '(cd ./integrativeTests/collections && npm run tests-all)'
                currentBuild.result = 'SUCCESS'
            } catch(Exception e) {
                currentBuild.result = 'FAILURE'
            }
            sh 'junit-viewer --results=./integrativeTests/collections --save=result.html'
            archiveArtifacts artifacts: 'result.html', fingerprint: true
            junit '**/integrativeTests/collections/COLLECTION-*.xml'
        }
    }
    ...