Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the number of tests run in Jenkins with JUnit XML format in the post job script?

Is there any way to get the number of tests that were executed (or passed) in Jenkins in a Post Job Script (for example, to send this number to the Github Status API)?

like image 435
Javier Soto Avatar asked Mar 29 '13 22:03

Javier Soto


Video Answer


2 Answers

I didn't see any way to access these numbers directly from Publish JUnit test result report Jenkins plug-in.

However, you can always use/parse the xml or json taken form Jenkins REST API after successfully parsing the JUnit XML:

http://<jenkinsHost>/job/<YourJobName>/<JobID>/testReport/api/json?pretty=true or

to have it more generic: http://<jenkinsHost>/job/<YourJobName>/lastSuccessfulBuild/testReport/api/json?pretty=true

for JSON output:

  {
      "duration" : 6109.1104,
      "failCount" : 0,
      "passCount" : 4389,
      "skipCount" : 0,
      "suites" : [
        {
        "cases" : [
          {
            ...
          }
        ],
        "duration" : 0.012,
        "id" : null,
        "name" : "EventTest",
        "stderr" : null,
        "stdout" : null,
        "timestamp" : null
         }
       ]
  }

http://<jenkinsInstanceHost>/job/<YourJobName>/<JobID>/testReport/api/xml

for XML output:

<testResult>
  <duration>6109.1104</duration>
  <failCount>0</failCount>
  <passCount>4389</passCount>
  <skipCount>0</skipCount>
 <suite>
  <case>
    <age>0</age>
    <className>
     ...
    </className>
    <duration>0.012</duration>
    <failedSince>0</failedSince>
    <name>Loop</name>
    <skipped>false</skipped>
    <status>PASSED</status>
  </case>
  <duration>0.012</duration>
  <name>EventTest</name>
 </suite>
</testResult>
like image 176
Blaise Avatar answered Sep 21 '22 11:09

Blaise


I ended up just writing a simple bash scrip that does super simple parsing of the xml produced after running the tests. Using the API as the other answer suggests is a cool idea if you need to do this remotely. In my case, this is the script:

total_tests=$(cat test-reports/*.xml | grep "<testsuite" | tr -s " " | cut -d " " -f6 | cut -d "=" -f2 | sed 's/\"//g' | paste -sd+ - | bc)
like image 43
Javier Soto Avatar answered Sep 18 '22 11:09

Javier Soto