trouble including PHPUnit

2019-06-23 23:38发布

问题:

I want to start writing tests for my code so I installed the latest PHPUnit with the following commands

wget http://pear.phpunit.de/get/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit

http://phpunit.de/manual/3.8/en/installation.html

then I added /usr/local/bin to my php.ini include path which looks like this

include_path = ".:/Applications/MAMP/bin/php/php5.4.10/lib/php:/usr/local/bin"

but I'm getting the following errors when I visit /my-app/test.php

Warning (2): include(PHPUnit/Autoload.php): failed to open stream: No such file or directory [CORE/Cake/TestSuite/CakeTestSuiteDispatcher.php, line 150]

Warning (2): include() [function.include]: Failed opening 'PHPUnit/Autoload.php' for inclusion (include_path='/Users/DevinCrossman/Sites/studio-bliss/lib:.:/Applications/MAMP/bin/php/php5.4.10/lib/php:/usr/local/bin') [CORE/Cake/TestSuite/CakeTestSuiteDispatcher.php, line 150]

I tried changing /usr/local/bin/phpunit to /usr/local/bin/PHPUnit but that didn't work. I also tried changing the include path from /usr/local/bin to /usr/local/bin/phpunit and restarted apache (I'm using MAMP PRO on this machine but it also didn't work on my ubuntu server)

a phpinfo() shows the include path is being set correctly.

it's probably something obvious that I've missed. Can anyone tell me why this isn't working?

回答1:

CakePHP recommends using PEAR to install PHPUnit. It should work better in your case.

Putting the phar in /usr/local/bin with filename 'phpunit' is meant to be used by running PHPUnit's testrunner via the command line as it will make the phpunit command available. e.g. running command:

phpunit MyTest test.php

When running in the browser, you need the PHPUnit classes available to load which does not work by simply having the path to your phar in the include path. I'm not sure but it may work if you include the phar in your test file:

require_once '/path/to/phpunit.phar'


回答2:

Edit or create composer.json in app directory. Add following lines.

{
    "require-dev" : {"phpunit/phpunit":"3.7.*"}
}

Then install using composer.

composer install