Javier Soto
Javier Soto

Reputation: 4870

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

Upvotes: 12

Views: 11758

Answers (2)

Javier Soto
Javier Soto

Reputation: 4870

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)

Upvotes: 3

Blaise
Blaise

Reputation: 7518

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>

Upvotes: 19

Related Questions