Configure Eclipse for PHPUnit

From PKP Wiki
Revision as of 10:41, 25 May 2012 by Jmacgreg (Talk | contribs) (Reverted edits by (talk) to last revision by

Jump to: navigation, search

Why using PHPUnit with Eclipse?

Tristar IT Support has provided IT Support Watford and specifically IT Support in Watford for an number of years. We cover all areas of Watford both remotely and with our trained IT Support engineers. One of the nice things about unit tests is that they help to develop higher quality code. A precondition for an efficient test first programming approach is good integration of the test framework into the development environment.

I am showing here how to integrate our test framework with Eclipse. If you don't use Eclipse then please feel free to add similar configurations for other CASE tools.

I have the following development environment:

  • Eclipse Galileo with PDT 2.0
  • PHPUnit 3.4
  • PHP 5.2.11

If you have not yet installed your PHPUnit test environment, then do so first. See our Unit Tests documentation for a step-by-step explanation.

Build Environment

First make sure that your project has the correct include and build path. Right click on your project in the navigator and select "Properties". Then navigate to the "PHP Include Path" preference page. Click on the "Libraries" tab, then click "Add Library...":

PHPUnit Eclipse IncludePath AddLibrary.jpg

Click on the "Next" button:

PHPUnit Eclipse IncludePath AddUserLibrary.JPG

Click on the "Config" button:

PHPUnit Eclipse IncludePath AddUserLibraryNext.JPG

In the preferences window click on the "New..." button:

PHPUnit Eclipse IncludePath AddUserLibraryPreferences.JPG

Enter the library name "PEAR" and add it to the environment.

PHPUnit Eclipse IncludePath AddUserLibraryName.JPG

After returning to the preferences page click the "Add external folder..." button:

PHPUnit Eclipse IncludePath AddUserLibrary AddExternalFolder.JPG

Find your PEAR folder (usually you find it in your PHP directory):

PHPUnit Eclipse IncludePath AddUserLibrary AddExternalFolderSel.JPG

NB: On Debian derivatives you probably have to add the php folder (e.g. /usr/share/php) rather than the PEAR folder as most PEAR packages (including PHPUnit) are directly installed into /usr/share/php.

Return to the preferences page and click "Ok":

PHPUnit Eclipse IncludePath AddUserLibraryOk.JPG

Finish the addition of the library

PHPUnit Eclipse IncludePath AddUserLibraryFinish.JPG

Once you returned to the project properties, please make sure that you've your project source in the include path:

PHPUnit Eclipse IncludePath Source.JPG

Then switch to the build path configuration and make sure your project appears there as well:

PHPUnit Eclipse BuildPath.JPG

Congratulations! You've successfully configured your build path.

Configure PHPUnit as Run/Debug Configuration

NB: This documentation has been updated for Eclipse Helios. If you have an older Eclipse version then your screens may look different.

We'll configure PHPUnit as a "Run/Debug Configuration" for quick and easy test execution from within Eclipse.

First select "Run Configurations..." from the "Run" menu:

PHPUnit Eclipse RunConfig.jpg

Go to the "PHP Script" tab and enter your configuration similar to the one shown in the following screenshots. Make sure you've got the correct paths for your environment:

PHPUnit Eclipse RunConfig Main.jpg

You may have to configure a PHP executable:

PHPUnit Eclipse DebugConfig PHPexcutables.JPG

Returning to the "Run Configurations" Wizard you can now configure the "PHP Script Arguments" arguments as so:

PHPUnit Eclipse RunConfig Args.jpg

Once you've done that, go to the "Environment" tab and add an environment variable as shown below:

PHPUnit Eclipse RunConfig Env.jpg

Click "Apply" and "Close" and that's it.

Configure XDebug Debugger to work with PHPUnit

If you have XDebug installed then you can also use the debugger with your tests. This comes in handy if you discover an error in your class while testing. I usually use the debugger more while unit testing than in an integrated environment. If you do good unit testing, integration bugs will appear a lot less frequently than before anyway.

The steps described above for adding a "Run Configuration" automatically also added a corresponding "Debug Configuration". If everything is set up correctly then you'll immediately be able to use XDebug to step through your tests.

Once you've accomplished the configuration steps on this page you may want to return to the Unit Tests page to write and execute your first test.