15

We are looking at switching from phpundercontrol to Hudson (it looks to have some really cool features!) but I can't figure out how to get phpunit logs to show up.

I have phpunit running fine in Hudson with ant, and --log-xml is putting a phpunit.xml in the appropriate builds/ folder for that build. But I can't figure out how to get that to show up for the build, so that we can see the tests the ran and which failed, if any.

After I figure that out, getting coverage, metrics, and api will be next :)

It seems like it should be trivial for anything which generates its on HTML, to tell Hudson for example "For this project show a link to 'API' for each build and link to builds/$BUILDNUM/api/index.html".

1
  • 1
    FYI...the --log-xml flag is deprecated. Use --log-junit instead. Sep 10, 2010 at 15:19

9 Answers 9

24

I installed the xUnit plugin, pointed it at my log file (from job config), and it works like a charm. It appears there is no longer a need for any custom hacks.

http://wiki.hudson-ci.org/display/HUDSON/xUnit+Plugin

1
  • Thanks, now that this plug-in exists it is indeed the proper solution as it handles the transformation for you.
    – mrooney
    Oct 9, 2009 at 21:29
7

With the last answer being from 2009 and a lot of people migrating from Hudson to Jenkins now due to Oracle, consider using the Jenkins Template for PHP, offering a free and convenient template for all your configuration needs of PHPQATools, like pdepend, phpmd, phpcs and phpunit in one convenient template.

There is also the IRC channel #jenkins-php on Freenode for support.

5

you can also use this xslt file to convert phpunit xml to hudson/junit xml:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <xsl:element name="testsuites">
        <xsl:for-each select="//testsuite[@file]">
             <xsl:copy-of select="." />
        </xsl:for-each>
    </xsl:element>
</xsl:template>
</xsl:stylesheet>
0
4

I typically work with CruiseControl for PHP testing (using CC's Phing support, not phpundercontrol). I have only worked with Hudson a little, but have gotten Hudson to successfully record phpunit tests using phing's phpunit support.

The following instructions assume that you will be using Phing (not Ant) to manage your PHP project builds and that you have the necessary prereqs installed. It also assumes you have PHPUnit 3 installed (though PHPUnit 2.x should work too).

Step 1: Setup Project for Phing/PHPUnit

First you need to make sure that your project is testable using Phing. Here's a sample Phing build.xml that runs unit tests and creates a JUnit-compatible XML output.

<?xml version="1.0" ?>
<project name="Test Project" default="test">
    <property name="tests.dir" value="." />
    <property name="reports.dir" value="${tests.dir}/reports" />

    <target name="test" description="Run PHPUnit tests">
        <phpunit haltonerror="true" haltonfailure="true" printsummary="true">
            <batchtest>
                <fileset dir="${tests.dir}">
                    <include name="**/*Test.php" />
                </fileset>
            </batchtest>
            <formatter type="plain" usefile="false" />
            <formatter type="xml" usefile="true" todir="${reports.dir}" outfile="test-results.xml" />
        </phpunit>
    </target>
</project>

Step 2: Setup Hudson

  1. Install the Phing plugin for Hudson. Note that you may need to install Phing as a standalone pacakge (I put it in /opt/phing-2.3.3) and configure PHING_HOME in Hudson config to point to that directory. You should also be able to use the PEAR-installed Phing; however, I have not tested that.
  2. Configure Hudson to build your project using Phing.
  3. Configure Hudson to collect JUnit tests from your project. The name of our results file will be test-results.xml. In the example above you'd configure it to collect files from reports/*.xml.

Step 3: Build!

That should do it. Try building your project now. Hopefully it will collect the results.

Also see this Phing presentation, or this Phing presentation for more on Phing and (to a lesser extent) PHPUnit integration.

Good luck!

3
  • Thanks for the answer! I was hoping to do it using ant as phing is not installed, but I will give this a shot and report back.
    – mrooney
    Feb 6, 2009 at 19:08
  • Unfortunately this isn't working. I have it working with phing and it is outputting the xml files where I expect, although the JUnit publisher complains "None of the test reports contained any result" just like it did before, even though it is pointing at the new directory. Any ideas?
    – mrooney
    Feb 6, 2009 at 20:10
  • Hmmm ... that's odd. This definitely was working for me -- latest version of all products involved. Does the resulting XML file actually contain (valid) test results? Did you double check the directory path? Some paths in Hudson seem to be relative to the workspace root, some to project root.
    – Hans L
    Feb 9, 2009 at 3:45
2

The format of the XML emitted by PHPUnit is not (currently) compatible with Hudson because it is not quite the same as the XML generated by other similar tools. That's why you get the "None of the test reports contained any result" message.

Short of fixing PHPUnit to generate "better" XML or improving Hudson to be more flexible in what it accepts, the only solution is to fix the XML by eliminating the nesting of <testuite> elements. I've used sed to alter the PHPUnit XML so that it is acceptable to Hudson:

# Tweak the test result XML to make it acceptable to Hudson.
lines=`wc -l test-results/results.xml|awk '{print $1}'`
end=`expr $lines - 1`
sed -i "$end d;3d" test-results/results.xml
1
  • Thanks Dan. I responded to your blog post which didn't seem to work for me, I think because I have more than 2 levels of nested testsuite tags. Any other tips? Hopefully someone will come along with a phunit plugin at some point.
    – mrooney
    Apr 1, 2009 at 22:27
2

The XSLT transformation from the previous post doesn't works for me. After applying this transformation DOM structure of the report is not changed. I've modified it to fix the problem. My variant is:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
      <xsl:element name="testsuites">
         <xsl:for-each select="//testsuite[@file]/testsuite">
            <xsl:copy-of select="." />
         </xsl:for-each>
      </xsl:element>
   </xsl:template>
</xsl:stylesheet>

it works for me.

2

If you would like a complete tutorial on integrating PHP with Hudson you can check out this link:

Continuous Integration for PHP with Hudson

0

The answer above is valid but more simply, without changing your build process :

0

I believe the xslt might have to be varied depending upon the phpunit version one is using. For instance, the style sheet posted by hoschi worked well for me, using phpunit version 3.3.17

But it's entirely possible that phpunit was upgraded to emit a different format in newer versions, or sir-vestnik could be running an older version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.