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):
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 an External Tool
We'll configure PHPUnit as an "External Tool" for normal test execution. We don't use a "Run Configuration" here because Eclipse sometimes does not show the full script console output when configured as a "Run Configuration".
First select "External Tool Configurations.." from the "Run" menu:
Go to the "Main" tab and enter your tool data similar to the ones shown in the following picture. Make sure you've got the correct paths for your environment:
Once you've done that, go to the "Common" tab and make the necessary selections as shown below:
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.
First select "Debug Configurations..." from the "Run" menu:
Enter your debug configuration data as shown in the image below. Then you'll have to configure your runtime executable if you've not done so before. Click on the "PHP Executables..." link:
You'll have to configure your PHP executable as shown here. Please make sure you enter the correct paths for your environment:
Returning to the Debug Configuration Wizard you can now configure the script arguments as so:
Finally you switch to the commons tab and make sure that your configuration will appear on the favorites menu. Also make sure you tick the "Allocate Console" checkbox. Otherwise you won't see test output:
Finally "Apply" and "Close" your debug configuration.
You may now return to the Unit Tests page to write and execute your first test.