12

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)?

Javier Soto
  • 4,840
  • 4
  • 26
  • 46

2 Answers2

19

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>
Blaise
  • 7,230
  • 6
  • 43
  • 53
  • It's a lot better to parse the JSON, I didn't know the Jenkisn API could give you that. Marking as good answer, thanks! – Javier Soto Jul 10 '13 at 18:56
3

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)
Javier Soto
  • 4,840
  • 4
  • 26
  • 46