开发者

Debug some PhpUnit tests in Eclipse

I use Eclipse PDT for PHP. I can run my PhpUnit tests : works fine.

But I can not debug my unit tests. Has someby already done this ? Can somebody h开发者_如何学Pythonelp doing this ?

Thanx, Messaoud


An example is more worth than 1000 words :

require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/TextUI/TestRunner.php';

class MyTestCase extends PHPUnit_Framework_TestCase {

 protected function setUp() {
  parent::setUp ();

 }

 function testSimple() {
  echo "horray !";
 }

 protected function tearDown() {

  parent::tearDown();
 }

 static function main() {

  $suite = new PHPUnit_Framework_TestSuite( __CLASS__);
  PHPUnit_TextUI_TestRunner::run( $suite);
 }
}

if (!defined('PHPUnit_MAIN_METHOD')) {
    MyTestCase::main();
}

the key thing is :

  1. provide a main method in your testcase

  2. test if the test is executed directly (via php MyTestCase.php) or by phpunit itself. if executed directly - just start the testrunner.

know you can debug your testcase.


We can solve this issue with our Eclipse plugin MakeGood.

MakeGood provides a simple way to debug your tests. You only run a test in Debug Mode. For more information, see the user guide.

Debug some PhpUnit tests in Eclipse


For others who are wondering if there are simple instructions for configuring Eclipse/Aptana with phpunit, here's a website I have found:

http://pkp.sfu.ca/wiki/index.php/Configure_Eclipse_for_PHPUnit

What you have to do basically is:

  1. Make sure your PEAR libraries are in your project's include path. Right click on the project in the navigator window and click Properties. You'll see there's a section for PHP Include Path (or PHP Build Path in Aptana for my version), open that and add your PEAR libraries to your include/build path so that Eclipse knows about phpunit.
  2. Create a debug configuration which runs the phpunit.php file (you might need to add the .php extension to the file if it is running with a shebang, as the case is in Mac OS X). So with phpunit.php file as the "Start Action" script, set "PHP Script Arguments" so that the PHPUnit testing file you are interested with is run by phpunit.php. Add any other command line arguments, to suit you. eg. --verbose is a good option. You can also use variables like ${resource_loc} to have Eclipse replace it with the current file for example.
  3. Run your debug configuration and enjoy debugging!

You do not need to modify your test files or anything, they'll work out of the box.


I finally run debugging parallel to command line in eclipse 3.4. Debugging i run as "PHP web page", my minimal code

require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/TextUI/TestRunner.php';
class XTest extends PHPUnit_Framework_TestCase{
    public function testX(){
        //...
    }
}
if (!defined('PHPUnit_MAIN_METHOD')) { 
    header('Content-type:text/plain; charset=utf-8');
    PHPUnit_TextUI_TestRunner::run( new PHPUnit_Framework_TestSuite( 'XTest'));
}


I have confirmed by setting a breakpoint in my setUp() method inside my unit test by following the instructions here:

How to Debug Your PHP Unit Tests in Eclipse

It involves copying the /usr/bin/phpunit file to your project (so it's accessible through eclipse's GUIs), and add the .php extension to it. From there, goto your debug configs and set the PHP-File to that phpunit.php file.

The next important step worked great for me, because I'm using Yii which provided me with a bootstrap.php file. Put something like this in your args:

--bootstrap=${workspace_loc}/my-project/trunk/protected/tests/bootstrap.php ${workspace_loc}/my-project/trunk/protected/tests/unit/SomeClassToTest.php
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜