Difference between revisions of "Configure Eclipse for PHPUnit"

From PKP Wiki
Jump to: navigation, search
m (Bug fix: copy&paste error)
(Removed spam)
 
(12 intermediate revisions by 5 users not shown)
Line 9: Line 9:
 
* PHPUnit 3.4
 
* PHPUnit 3.4
 
* PHP 5.2.11
 
* 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 ==
 
== Build Environment ==
Line 39: Line 41:
  
 
[[Image:PHPUnit_Eclipse_IncludePath_AddUserLibrary_AddExternalFolderSel.JPG]]
 
[[Image: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":
 
Return to the preferences page and click "Ok":
Line 58: Line 62:
 
Congratulations! You've successfully configured your build path.
 
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".
+
== Configure PHPUnit as Run/Debug Configuration ==
  
First select "External Tool Configurations.." from the "Run" menu:
+
NB: This documentation has been updated for Eclipse Helios. If you have an older Eclipse version then your screens may look different.
  
[[Image:PHPUnit_Eclipse_ExtToolConfig.JPG]]
+
We'll configure PHPUnit as a "Run/Debug Configuration" for quick and easy test execution from within Eclipse.
  
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:
+
First select "Run Configurations..." from the "Run" menu:
  
[[Image:PHPUnit_Eclipse_ExtToolConfig_Main.JPG]]
+
[[Image:PHPUnit_Eclipse_RunConfig.jpg]]
  
Once you've done that, go to the "Common" tab and make the necessary selections as shown below:
+
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:
  
[[Image:PHPUnit_Eclipse_ExtToolConfig_Common.JPG]]
+
[[Image:PHPUnit_Eclipse_RunConfig_Main.jpg]]
  
Click "Apply" and "Close" and that's it.
+
You may have to configure a PHP executable:
  
== Configure XDebug Debugger to work with PHPUnit ==
+
[[Image:PHPUnit_Eclipse_DebugConfig_PHPexcutables.JPG]]
  
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.
+
Returning to the "Run Configurations" Wizard you can now configure the "PHP Script Arguments" arguments as so:
  
First select "Debug Configurations..." from the "Run" menu:
+
[[Image:PHPUnit_Eclipse_RunConfig_Args.jpg]]
  
[[Image:PHPUnit_Eclipse_DebugConfig.JPG]]
+
Once you've done that, go to the "Environment" tab and add an environment variable as shown below:
 +
 
 +
[[Image:PHPUnit_Eclipse_RunConfig_Env.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:
+
'''Replace the variable value to "env1".'''
  
[[Image:PHPUnit_Eclipse_DebugConfig_PHPScript.JPG]]
+
Click "Apply" and "Close" and that's it.
  
You'll have to configure your PHP executable as shown here. Please make sure you enter the correct paths for your environment:
 
  
[[Image:PHPUnit_Eclipse_DebugConfig_PHPexcutables.JPG]]
+
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".
  
Returning to the Debug Configuration Wizard you can now configure the script arguments as so:
+
== Configure XDebug Debugger to work with PHPUnit ==
  
[[Image:PHPUnit_Eclipse_DebugConfig_ScriptArgs.JPG]]
+
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.
 
+
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:
+
  
[[Image:PHPUnit_Eclipse_DebugConfig_Common.JPG]]
+
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.
  
Finally "Apply" and "Close" your debug configuration.
 
  
You may now return to the [[Unit Tests]] page to write and execute your first test.
+
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.

Latest revision as of 15:04, 7 November 2012

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

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

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.