keskiviikko 21. joulukuuta 2011

How to use Phing to run PHPUnit scripts and generate HTML reports

Phing is a really neat application build and deployment tool that you can also use to run PHPUnit tests. What is so nice about it is the simplicity of the build script and the possibility to obtain nicely formatted HTML report of the test scripts.

You can manually generate HTML report with PHPUnit by using an extra switch to generate the results in XML format, like so:

$ phpunit --log-junit report.xml MyApplication

MyApplication is the directory where all the test scripts reside and report.xml is the output report file in XML file. Now it is up to you to either manually parse the XML file and generate HTML report based on it or you can use XSLT to transform the XML to nice HTML web report.

As I said before, why invent the wheel again when you can let Phing do this for you automatically. Before we build our first build file for Phing, we need to install the Phing itself in case you haven't done so yet. I take it you already have PEAR installed so all we have to do is enter the following:

$ sudo pear channel-discover pear.phing.info
$ sudo pear install phing/phing

After Phing has been installed successfully, all you have to do is enter 'phing' in the command line. This will bring you 'Buildfile: build.xml does not exist!' error message because Phing tries to open build.xml in the directory where Phing is called in. So off we go create our build.xml file to run our PHPUnit scripts.

So we are in a directory that contains a subdirectory called MyApplication. This directory has all the test scripts. Create a file called build.xml and copy/paste the following into it:

<project name="MyApplication" default="build">
  <property name="package" value="MyApplication" override="true" />
  <target name="clean">
    <delete dir="build"/>
  </target>
  <target name="prepare">
    <mkdir dir="build/logs"/>
  </target>
  <target name="phpunit">
    <phpunit printsummary="true" haltonfailure="false">
      <formatter todir="build/logs" type="xml"/>
      <batchtest>
        <fileset dir=".">
          <include name="MyApplication/*Test.php"/>
        </fileset>
      </batchtest>
      <formatter type="xml" todir="report" outfile="logfile.xml"/>
   </phpunit>
   <phpunitreport infile="report/logfile.xml"
                  styledir="/usr/share/php/data/phing/etc"
                  format="frames"
                  todir="report"/>
  </target>
  <target name="build" depends="clean,prepare,phpunit"/>
</project>

If everything goes well, Phing will scan for *Test.php files in the MyApplication directory and run their tests. After the test run, an HTML report package is created into subdirectory called report.

Depending on your environment, you might have to edit the styledir="/usr/share/php/data/phing/etc" portion of the above script to reflect the path where the XSL files for the phing reside.

To make the most use of the HTML report page, set the Phing script to create it within your server's web folder so that you can access it with your browser. Now you can schedule a daily or hourly test run and you will have updated report of your deployed application's test run whenever you need one.

Ei kommentteja:

Lähetä kommentti