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.

tiistai 20. joulukuuta 2011

Running Selenium tests on PHPUnit

Lately I have been dealing a lot with PHPUnit testing of variety of applications. Main focus has always been on testing the backend classes for applications, but lately I have learned the importance of testing also functional aspect of a web page.

For years I have generated functional test scripts with Firefox's add-on called Selenium IDE, which allows you to record page clicks and text inputs and then run the recording as many times as you wish.

Now, to make full potential of Selenium, one should really take advantage of running Selenium test scripts in PHPUnit to combine both backend testing as well as functional testing. First you need to install Selenium RC server which is used to launch a web browser of your choice and do the things you recorded.

Requirements:

- PHP (obviously)
- PHPUnit
- PHPUnit_Selenium (an extension for PHPUnit)
- Selenium RC server
- Internet browser (Firefox/Chrome/Internet Explorer)

Let's start off with installing PHPUnit from PEAR if you have not done so yet.

$ sudo pear upgrade pear
$ sudo pear channel-discover pear.phpunit.de
$ sudo pear channel-discover components.ez.no
$ sudo pear channel-discover pear.symfony-project.com
$ sudo pear install --alldeps phpunit/PHPUnit

Now we need to make sure we install also the Selenium extension to PHPUnit.

$ sudo pear install --force phpunit/PHPUnit_Selenium

In the above command line, we used --force switch to ignore a possible error message about Curl not being installed.  Selenium extension requires that Curl is installed so make sure it is.

Now that we have installed all required PHP classes and extensions, it is time to move on to the Selenium RC server which we connect to each time we run Selenium tests in PHPUnit.

First, browse to http://code.google.com/p/selenium/downloads/list click on selenium-server-standlone-2.*.jar (at the time of writing) link, and then copy the url of the selenium-server-standlone-2.*.jar file.

Next, open up a terminal window and type:

$ sudo su
$ mkdir /usr/lib/selenium/
$ cd /usr/lib/selenium/
$ wget url-you-copied-above
$ mkdir -p /var/log/selenium/
$ chmod a+w /var/log/selenium/

Next, take this attachment, and save it as /etc/init.d/selenium

Now edit the file, and change the filename of the standalone server file (currently selenium-server-standalone-2.0a5.jar) to the name of the server file you downloaded above.

Finally, make the script executable:

$ chmod 755 /etc/init.d/selenium

To start Selenium RC server:

$ /etc/init.d/selenium start

Finally, add the script so that it starts automatically when the server does:

$ update-rc.d selenium defaults
Congratulations, you have now done all the preliminary configurations to get the testing environment up and running.

maanantai 19. joulukuuta 2011

My words of welcome

Welcome to my PHP development blog, where I will write down things about PHP programming, techniques and other related issues.

The main purpose of this blog is to write down all the things for myself so I can locate all important information in one place, but I also want to share these things I have learned and developed with you.

I hope you will find the articles in this blog useful for you and your fellow team members. Please feel free to post comments if you have any concerns and questions about my articles.

So who am I?

My name is Artur Gajewski and I live in Helsinki, Finland. I have been doing development in PHP and J2EE since 2000. PHP is my main focus currently and in 2010 I received certification for Zend Framework. Currently I am working on becoming certified in PHP 5.

Currently I work as a web developer for a company called Codemate, which is an outsourcing company based in Finland and Bangladesh.