1 min read

Ant Groovy target to create Hudson compatible PHPUnit XML report

Ant Groovy target to create Hudson compatible PHPUnit XML report
Photo by Vlad Tchompalov / Unsplash

While setting up a Hudson CI and deployment solution for some PHP projects I ran into a problem getting the PHPUnit reports to integrate properly into the builds.

I kept getting the error:

Recording test results
None of the test reports contained any result

It seems that the report XML that PHPunit creates with the option for --log-junit is formatted differently than Hudson expects; adding extra "test case" nodes.

After some research I found a thread discussing the problems with some users providing an XSLT file to convert the PHPUnit format to a format Hudson will understand.

With this information, I added an Ant target in my CI build.xml workflow to transform the XML using a Groovy Ant task.

I call this task after my PHPUnit task has created my --log-junit report named junit-phpunit.xml and save the transformed XML to my final junit.xml that I have configured Hudson's post-build "Publish JUnit test result report" action to use.

import java.xml.transform.TransformerFactory 
import javax.xml.transform.stream.StreamResult 
import javax.xml.transform.stream.StreamSource 

def xslt = ''' ''' 
def factory = TransformerFactory.newInstance() 
def transformer = factory.newTransformer( 
    new StreamSource(
        new StringReader(xslt)
    ) 
)
transformer.transform( 
    new StreamSource(new File("${properties.basedir}/build/logs/junit-phpunit.xml")), 
    new StreamResult(new File("${properties.basedir}/build/logs/junit.xml"))
)