Search code examples
xmlbashshellsh

How to extract the test results from testng.xml file using bash


I am trying to extract the test results from testng.xml file using shell commands

<?xml version="1.0" encoding="UTF-8"?>
<testng-results skipped="0" failed="0" ignored="0" total="1" passed="1">
  ....................
</testng-results>

From the above the xml file I want to extract an output as

Total : 1
Passed : 1
Failed : 0
Skipped : 0
Failed : 0

Can someone help me on this?


Solution

  • With xmllint and xpath in a shell, with variables:

    $ for str in total passed failed skipped; do
        xmllint --xpath "concat('${str^}: ', string(/testng-results/@$str))" file.xml
      done
    
    Total: 1
    Passed: 1
    Failed: 0
    Skipped: 0