TDD Part 1: Programs to check your programs: PHPUnit and Selenium

Accessibility resource testing

Post Tags



Developing applications can have this overwhelming feeling of keeping a lot of balls in the air…or herding cats…or wrestling an octopus. This feature touches that feature which impacts that other feature. Each of those features may have a multitude of cases that will behave in different ways depending on usage, conditions, etc. And so when you touch a piece of this interconnected network, it can have a rippling effect, breaking things you never would have expected.

Enter automated testing, which involves writing programs to check your programs. That way, as you’re working along you have tests in play that are following behind you making sure you didn’t break anything.

There’s unit testing… where you write methods to test specific methods, making sure the expected results are returned.

    function get_user($user_id) {
    // Imagine code here that gets the user

    // Return user's array
    return $user;

function test_get_user() {
    $user = get_user(97);
    $this->assertIsA($user, array);
    $this->assertTrue($user['first_name'], "Sally");

And then there’s acceptance / functional testing which is more bigger picture testing. It doesn’t ask “Is this individual method returning what we’d expect?” but instead asks “After the user logs in, are they making it to their dashboard?”

In order to do this kind of testing, you need to get set up with a testing framework such as PHPUnit. PHPUnit will give you unit testing right out of the gate…but if you want to expand to acceptance testing you can plug it into Selenium which automates browser processes. This means you can make Selenium be your little test robot making sure all the parts of your application work as expected.

NetTuts has a nice 20 min screencast covering the PHPUnit / Selenium duo

How to use Selenium 2 and PHPUnit.

Because this tutorial assumes you already have PHPUnit up and running I had to do a little digging around in the first five minutes of the tutorial. I’ve gathered those pieces, as well as the other pieces of the tutorial and summarized the step-by-step. The following is meant to supplement that screencast (not replace it).

Extra Resources:

I ran the following on MAMP Pro 2.1.1 running php5.4.4.

Make sure PEAR Is installed

Open terminal, run this command:

$ which php

If you get /usr/bin/php that’s the default PHP path; we need it to point to the MAMP PHP Path.

Launch editor to change this via .profile:

$ edit ~/ .profile

Add this line (replace with whatever version of php you’re running)

export PATH=/Applications/MAMP/bin/php/php5.4.4/bin:$PATH >> ~/.profile

Now run this to refresh the path:

$  . ./.profile

You should now have the correct binaries:

$ which php

$ which pear

$ which pecl
Install PHPUnit via PEAR

Install PHPUnit via Pear

Run these commands:

$ pear config-set auto_discover 1
config-set succeeded
$ pear install

Install and run Selenium Server

Download the Selenium Server from here:

Move the selenium-server-standalone-2.25.0.jar file into /Applications/MAMP/bin/php/php5.4.4/bin

Run this command to test it:

$ java -jar /Applications/MAMP/bin/php/php5.4.4/bin selenium-server-standalone-2.25.0.jar

Create an alias for the Selenium Server

Open the .bash_profile file in an editor:

$ edit ~/.bash_profile

Add this line:

alias selenium="java -jar /Applications/MAMP/bin/php/php5.4.4/bin/selenium-server-standalone-2.25.0.jar"

Save, Quit and re-open terminal

This command should now start the selenium server:

$ selenium

Install Selenium Package


$ pear install phpunit/PHPUnit_Selenium

Prepare a sample project to test on

In your Sites / localhost directory create seleniumTut/ directory with the following files:

  • index.php
  • admin.php
  • testLogin.php


Run test on project

$ phpunit /Users/Susan/Sites/seleniumTut/testLogin.php

Comments are closed.