Difference between revisions of "Configure Eclipse for PHPUnit"

From PKP Wiki
Jump to: navigation, search
m (Added PHP_PEAR_BIN_DIR config for Linux)
Line 75: Line 75:
If you system does not have the PHP_PEAR_BIN_DIR environment variable defined then you now have to add this variable to the 'Environment' tab. Please enter the directory that contains the phpunit script. A common value on Linux is '/usr/bin'.
Click "Apply" and "Close" and that's it.
Click "Apply" and "Close" and that's it.

Revision as of 12:08, 16 June 2010

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.

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

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 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:

PHPUnit Eclipse ExtToolConfig.JPG

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. Note, if you receive Eclipse errors concerning ${project_loc} referencing an empty selection, there are two possibilities. The first is to simply re-run the command, making sure that your project is selected on the left in the navigator. The second is to use ${workspace_loc} instead, which appears to be less buggy:

PHPUnit Eclipse ExtToolConfig Main.JPG

Once you've done that, go to the "Common" tab and make the necessary selections as shown below:

PHPUnit Eclipse ExtToolConfig Common.JPG

If you system does not have the PHP_PEAR_BIN_DIR environment variable defined then you now have to add this variable to the 'Environment' tab. Please enter the directory that contains the phpunit script. A common value on Linux is '/usr/bin'.

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:

PHPUnit Eclipse DebugConfig.JPG

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:

PHPUnit Eclipse DebugConfig PHPScript.JPG

You'll have to configure your PHP executable as shown here. Please make sure you enter the correct paths for your environment:

PHPUnit Eclipse DebugConfig PHPexcutables.JPG

Returning to the Debug Configuration Wizard you can now configure the script arguments as so:

PHPUnit Eclipse DebugConfig ScriptArgs.JPG

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:

PHPUnit Eclipse DebugConfig Common.JPG

Finally "Apply" and "Close" your debug configuration.

You may now return to the Unit Tests page to write and execute your first test.