Configure Eclipse for PHPUnit
Why using PHPUnit with Eclipse?
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.
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...":
Click on the "Next" button:
Click on the "Config" button:
In the preferences window click on the "New..." button:
Enter the library name "PEAR" and add it to the environment.
After returning to the preferences page click the "Add external folder..." button:
Find your PEAR folder (usually you find it in your PHP directory):
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":
Finish the addition of the library
Once you returned to the project properties, please make sure that you've your project source in the include path:
Then switch to the build path configuration and make sure your project appears there as well:
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:
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:
You may have to configure a PHP executable:
Returning to the "Run Configurations" Wizard you can now configure the "PHP Script Arguments" arguments as so:
Once you've done that, go to the "Environment" tab and add an environment variable as shown below:
Replace the variable value to "env1".
Click "Apply" and "Close" and that's it.
You will need to setup two run configurations to be able to run all tests. The second one can be duplicated from the first one and you should only change the environment tab variable value from "env1" to "env2".
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.