Home > Uncategorized > Testing your PHP Code

Testing your PHP Code

September 19, 2011 Leave a comment Go to comments

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:

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

PHPUnit
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).

Atoum
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

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

Phake
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.

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

Behat
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:
      """
      bar
      foo
      """

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.

Mink
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.

Advertisements
Categories: Uncategorized Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: