Archive

Posts Tagged ‘phpunit’

Installing PHPUnit

January 19th, 2009 No comments

PHPUnit is a powerful unit testing framework written in and for PHP. Rather than testing everything as a whole the idea behind PHPUnit is to test that everything works as it is expected to work before it is integrated into the rest of the program. In this way problems are found earlier rather than later and this makes fixing them a lot easier. With tests written for every small component of the program it is then possible to test the whole thing by running all of the tests at once. It is also possible to automate PHPUnit so that everything about a program is tested before it is built. If any tests fail then the build is stopped.

The best way to install PHPUnit is to use the PEAR installer. To install PHPUnit you first need to add the correct channel so that PEAR can find PHPUnit easily. You do this by typing the following into the command line.

pear channel-discover pear.phpunit.de

You can now install PHPUnit by using the following command.

pear install phpunit/PHPUnit

You can now test your install by trying to run PHPUni through the command line.

phpunit

If properly installed this command will print out usage and parameter information for PHPUnit.

You can see which version of PHPUnit is installed by using the –version parameter.

phpunit --version

For more information about PHPUnit take a look at the PHPUnit website.

Categories: PHP Tags: , , , ,

Integrating Phing With PHPUnit

January 16th, 2009 No comments

PHPUnit is a unit testing framework, written in PHP, and which is used to test PHP code. You can integrate the testing that PHPUnit does into Phing. You might want to use Phing to create a nightly build that contains the latest version of your program. The last thing you want is Phing to create a nightly build that is riddled with errors.

The way around this is to use PHPUnit to test our code whilst we are running Phing. If any tests fail then Phing will not finish the build.

Create a target at the top of your build.xml file and make sure it is run first. Then add the following code to the target, This will use PHPUnit to test your code.

<phpunit haltonfailure="true" haltonerror="true">
  <batchtest>
    <fileset dir="tests">
      <include name="**/*Test*.php"/>
    </fileset>
  </batchtest>
</phpunit>

The phpunit element contains an element called batchtest, which can contain one or more fileset elements. It is the files defined by the fileset elements that define which files are to be used in the testing. The code above includes all PHP files that have the word Test in their name.

The phpunit element contains two attributes called haltonfailure and haltonerror. These attributes will cause the build to exit if any errors or failed tests are found during the testing.