Archive for September, 2011

Testing your PHP Code

September 19, 2011 Leave a comment

Depending on which circles you run in, hearing PHP will either make you cringe or bring you joy and comfort.   Another recently blogged on this topic and I really liked what he had to say.

Since he argued that point, I’m here to point out some of the testing libraries that are available for those of you using PHP.  You can see from the list below that PHP is gaining ground in an area many feel to be lacking.

In no particular order:

Provides unit testing, basic web functional testing, and a mock API.

I’ve been using PHPUnit for over 3 years now and it’s constantly improving.  It has strong community support and is backed by Sebastian Bergmann.   It provides unit testing, a mocking API, functional web testing via selenium,  database testing (DBUnit), code coverage generation utilizing xdebug, and a number of output formats easily consumable by various tools (My favorite: Jenkins).

A pretty new framework (May 2010) that promotes ease of use. It makes heavy use of a Fluent interface for writing tests.

It delivers itself in the form of a PHAR (PHP Archive) file and is run like this:

php mageekguy.atoum.phar –testIt

Easy to install via PEAR and integrates with PHPUnit as an alternative to the Mock API PHPUnit provides.

Not to be confused with phake, which is a rake/make equivalent for PHP, which is also what phing provides to the PHP community….

Capital P, Phake, is yet another mocking API built to overcome some of the short falls in other mock APIs.  mlively is a heavy contributor to PHPUnit’s DBUnit module and has written this module.

You guessed it, Ruby has RSpec and PHP has PHPSpec.  A BDD testing style framework.  I’ll let you read more about it.

Ruby has Cucumber, PHP now has Behat.  Behat was inspired by and tries to stay true to Ruby’s Cucumber approach.  I really like the “feature file” approach at this level of testing.  How easy is it to read and collaborate on test scenarios when they’re in plain English?

Example taken from their website.

Feature: ls
  In order to see the directory structure
  As a UNIX user
  I need to be able to list the current directory's contents

  Scenario: List 2 files in a directory
    Given I am in a directory "test"
    And I have a file named "foo"
    And I have a file named "bar"
    When I run "ls"
    Then I should get:

These feature files are backed by “step definitions” that carry out the actions described here.  By creating a DSL (Domain Specific Language) you could have almost anyone write and execute tests.

The makers of Behat also bring us Mink,  a web functional testing tool.   Supports both headless browser (HTTP request/response) and in-browser testing for validating functionality.  It provides driver abstractions that integrate with a number of other open source software to accomplish its goal.

Categories: Uncategorized Tags: ,