Running PHPUnit tests in Eclipse PDT

By | 22 August 2012 | Comment


Working with PHPUnit and Eclipse PDT has always been a bit of a pain. We ended up using it as an external tool, and it worked, but it wasn't pretty. Then one day, I discovered the  MakeGood plugin for Eclipse. Not only does it enable you to run tests directly within Eclipse, but you can also set it to use PHPUnit's configuration and bootstrap file - and this means you can debug tests in Eclipse in just the same was as any other PHP script, using the Xdebug PHP extension.

This is a lifesaver, once you've got it set up. But it does take a few steps to get the whole thing up and running, which can be tricky if you haven't done it before. So here a quick guide from the Loft folks.

1. Basics

We're assuming you're running some kind of PHP+HTTPd stack on your local machine, with Eclipse PHP Development Tools as your IDE - if not, you can get PDT here.

2. The PHP CLI

The PHP CLI is PHP's command line interface binary, which enables you to run PHP scripts from the command line, just like any other executable:

> php hello-world.php

You can install PHP CLI as package (php5-cli), while is comes a part of the Windows Xamp package. (Windows users may find the CLI easier to work with if you add its path to your system path - see Command Line PHP on Microsoft Windows.)

Note that CLI may use a different php.ini to the one your web server uses - if so, you'll need to configure your PHP extensions and other bits and bobs in there separately. To be sure, you can check on the command line:

> php -i

3. The PHP Xdebug extension

The PHP Xdebug extension adds a number of valuable debugging and code profiling features to PHP. You'll need to have it installed, configured and enabled with PDT debugging support. If you are having problems with Eclipse and Xdebug, check this handy blog post.

4. PEAR

PEAR is the PHP Extension and Application Respository. Much of PEAR's code has been eclipsed by the Zend Framework but it still has a number of valuable uses, one of which is for PHPUnit.

PEAR is no longer installed along with PHP, as it used to be - if don't have it, head to pear.php.net, where you can either read the installation manual, or if you're in a hurry simply download go-pear.phar and run:

> php /where_stored/go-pear.phar

Follow the PEAR install instructions, hitting Enter to accept the default configuration as you go. When you're done, you can test your installation by running PEAR from the command line, just like this:

> pear

5. PHPUnit

With PEAR installed, you can now install PHPUnit using the PEAR Package Manager - just follow the PHPUnit install instructions. This is quick and simple, you shouldn't have any problems.

6. MakeGood

MakeGood is an Eclipse PDT plugin that enables you run unit tests and track issues directly within Eclipse - so much quicker than heading to your browser or command line each time you tweak a bit of code, and it can be automated to run when you save a file.

To install MakeGood, open Eclipse then go to Help >  Install New Software…. Enter "http://eclipse.piece-framework.com/" into the Work With field, and hit Enter. In the list of features that appears, select the MakeGood package.

Installing MakeGood

Click Next and follow the on-screen instructions to start the installation. You should reach a screen asking you if you trust certificates for Piece Project - accept them all to complete your installation and restart Eclipse. If you get stuck during this process, refer to the Installation section of the MakeGood User Guide.

7. Configure Eclipse

MakeGood needs the PHP CLI mentioned above to run, and it looks for this in your Eclipse settings. So, in your Eclipse Preferences, head to PHP > PHP Executables, and add an entry with the name and path to the CLI.

For PHPUnit, you also need to add PEAR to your build path, also in your Eclipse Preferences: head to PHP > PHP Libraries. This enables you to make it available to all your projects.

8. Configure your Eclipse project

You also need to adjust as couple of settings for the projects you would like to run your unit tests again.

The first is to add PEAR to the include path for the project. This is to ensure that PHPUnit's PHPUnit_Framework_TestCase class is available to your project test classes. In Eclipse's PHP Explorer, select the project, then select File > Properties to view its project settings. Select PHP Include Path, then under the Libraries tab click Add Library… Choose the User Library option, and choose your PEAR library.

The second is to point MakeGood to your PHPUnit configuration file for the project. This is a simple XML configuration file, which you should set up according to the PHPUnit configuration docs. Head to the MakeGood section of your project settings, and under PHP Unit, enter the path to your PHPUnit configuration XML file.

9. And you're done!

Everything should be ready now. If you can't see the MakeGood tab already, browse to Window > Show View > MakeGood.

Now, when you open any test class in your project (you do have some, right?), you can run and debug tests just like any other script...

... and see the results ...

Testing in action

Happy TDD-ing!

Thoughts, problems, opinions, other ideas?

Please share below...

Topics: PHP PHPUnit TDD

comments powered by Disqus