Installing PEAR, PHPUnit and Selenium on Windows

I wrote this for use at work, hence some of the wording. I may re-edit it one day, but it should do the trick. Feel free to leave any comments or questions below.

Installing PHPUnit on Windows is done with PEAR.

Installing PEAR

  1. Create the folder c:\program files\PEAR (This is your PEAR path)
  2. Download go-pear.phar from here: (the one which comes with PHP is usually broken) and save it in your PEAR path.
  3. Open a command prompt and navigate to your new PEAR folder, type php go-pear.phar
  4. Hit Enter to agree to a System install then Enter again to confirm all the settings are good (after checking them, of course).
  5. PEAR is now installed. When we use PEAR to install things, it creates a .bat files in our PEAR directory which can is used to run them. For example by the end of this page PHPUnit.bat will be installed and we can call it from the command line as ‘phpunit’ since windows is clever like that.

To be able to run these commands from outside of the PEAR directory we must add the PEAR directory to our PATH Environment Variable.

  1. Press Windows Key and Pause/Break to load the System screen.
  2. On the left click on Advanced System Settings.
  3. Click on the Advanced tab then “Environment Variables..” button at the bottom.
  4. In the bottom list, “System Variables” find and select the PATH variable and click Edit.
  5. Add a semi colon to the end of the list, and enter the path to your PEAR directory.
  6. Click OK and then OK again.

You will need to open a new CMD window if you already have one open so that you get one with the new environment variables available.

  1. Run “pear upgrade” to test and get the latest version in one handy step.

Installing PHPUnit

Navigate to your PEAR folder and run the following commands

  1. pear config-set auto_discover 1
  2. pear install --alldeps

PEAR creates a file called PHPUnit.bat, which we can run from anywhere because we put our PEAR directory into our PATH variable in the first step. It’s also created a bunch of other files which PHPUnit.bat uses.

You may wish to add C:\Program Files\PEAR\pear\PHPUnit to your php.ini include_path.

Installing Selenium for Integration Testing

Our Integration Tests use Selenium and involve launching a browser and running through things, they take siginificantly longer to run than Unit Tests and you’ll probably not run them every day. They’re useful when you’re changing the back end but not the UI, and will  need updating if you’re making changes to the UI. They also pick up JavaScript errors.

Selenium is a server to which PHPUnit will connect and issue commands, the server then launches a browser and tells it what to do.

  1. You need Java installed, take the Offline version from here:
  2. Download Selenium Server here: Create a folder and save this in c:\program files\selenium\
  3. Right click on your desktop, go to New > Shortcut. For Location simply type “Java”, hit Next and type Selenium Server as the name.
  4. Right click your new shortcut and click Properties, at the end of the “Target” field add -jar “C:\Program Files (x86)\Selenium\selenium-server-standalone-2.23.1.jar”
  5. Running this shortcut should give you a console screen with happy output.
  6. To install the PHPUnit Selenium extensions (to be able to run the Integration Tests) download this file (or check for a later version of PHPUnit_Selenium from here) and merge the PHPUnit_Selenium-1.2.7\PHPUnit\Extentions folder with it’s sister in your PEAR Directory under pear\PHPUnit

Note that we are using Selenium 2, so you must use the Selenium2 classes.

Running the tests

Running the tests should take a matter of seconds and are run from the command line. From within the Squadify code base cd into the tests folder, type “phpunit” and hit enter, you should see something like:

PHPUnit 3.5.15 by Sebastian Bergmann.
.... Errors Come out here ...
Time: 0 seconds, Memory: 5.25Mb

PHPUnit looks for a file called phpunit.xml from which it learns how you with the tests to run. There are command line options for running only sub sets of the tests specified, controlling the output and other cool things.

Writing or Editing Tests

Tests are seperated into TestCases, each TestCase will test one unit. Examples of units are models, pages and forms. Refer to PHPUnit’s documentation for specific details on how to write tests.

It’s very easy to write useless tests, some pointers to avoid doing so:

  • Before writing tests, write a Doc Comment description before the Test Case explaining why the test exists.
  • Write descriptive names for tests, similar to a Doc Comment these force you to think about what and why you’re testing.
  • You’re supposed to write tests that fail, this means running it so you can see it fail, then fix the code to make it pass. It’s important to remember that you must change the code being tested, not the test itself, for this.

For the sake of maintainability put as much care into reusing your test cases code as any other code, without violating these:

  • TestCases should never be dependant on another Test Case, individual tests can be when it makes lots of sense but it should be avoided. The reason is so that (for speed) you run a subset of the tests (hence the name Unit Test).
  • Factory methods (such as getEmptyUser() or getYoungUser()) should be begin with “get” and placed at the bottom of the class. They’re used to make maintaining easier.

More Information

PHP Unit Docs:
Advice on writing tests: