Getting PHPUnit Working - Include Path not set correctly?

56,725

Solution 1

If you installed phpunit correctly (through PEAR), then there is no need for an include file; you just need to change the way you use it to test php files (e.g. you use it to test if the file worked by going to the browser type localhost). With phpunit you can use the command line; chapter 5 gives the same example using the command line (I would assume it's a standard). So if you got it installed correctly you can do this:

  1. File ExampleTest.php, located at the root of localhost (for me this is /var/www):

    class ExampleTest extends PHPUnit_Framework_TestCase
    {
        public function testOne()
        {
            $this->assertTrue(FALSE);
        }
    }
    
  2. Open a console (terminal on Mac or Linux, command prompt on Win), navigate to your localhost document root (where you saved ExampleTest.php) and type the following:

    phpunit --verbose ExampleTest.php
    
  3. You should see:

    PHPUnit 3.4.13 by Sebastian Bergmann.
    
    F
    
    Time: 1 second, Memory: 6.50Mb
    
    There was 1 failure:
    
    1) ExampleTest::testOne
    Failed asserting that <boolean:false> is true.
    
    /var/www/ExampleTest.php:6
    
    FAILURES!
    Tests: 1, Assertions: 1, Failures: 1.
    

Notes: All the above assumes you installed phpunit correctly (as stated in chapter 3) and you restarted apache after that .

if you really want to run tests in your browser use the following code

# error reporting
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);

# include TestRunner
require_once 'PHPUnit/TextUI/TestRunner.php';

# our test class
class ExampleTest extends PHPUnit_Framework_TestCase
{
    public function testOne()
    {
        $this->assertTrue(FALSE);
    }
}

# run the test
$suite = new PHPUnit_Framework_TestSuite('ExampleTest');
PHPUnit_TextUI_TestRunner::run($suite);

Edit

Just spotted Ubuntu 10.10 in your question. For Ubuntu install I recommend this: in terminal do:

sudo pear uninstall phpunit/PHPUnit
sudo apt-get install phpunit
sudo /etc/init.d/apache2 restart

Note: Don't issue the first line if you didn't install phpunit through pear. The last line seems to be needed (in my case at least).

sudo /etc/init.d/apache2 reload # Or
sudo service apache2 restart

Solution 2

As of PHPUnit 3.5, you have to include the autoloader yourself:

require 'PHPUnit/Autoload.php'; // PEAR should be in your include_path

Solution 3

Add this line to your php.ini :

include_path=".:/usr/share/php:/usr/share/pear:/usr/share/php/PHPunit:/usr/share/php/PEAR"

Save the file and restart apache.

Also, make sure you editing the correct php.ini. Try using locate php.ini to find all the places it might be hiding. It's usually under the /usr directory somewhere.

Solution 4

Make sure every time to use include() or require(), you prefix the actual file name with dirname(__FILE__). This ensures that the file you are including is at the path you specify relative to the actual file that the include is in. By deftault, PHP includes relative to the file that is invoked to start the program.

Share:
56,725
Jarrod Nettles
Author by

Jarrod Nettles

Engineering Manager with Apple Maps.

Updated on July 05, 2022

Comments

  • Jarrod Nettles
    Jarrod Nettles almost 2 years

    I'm trying to get PHPUnit working on my development environment but I've hit a bit of a roadblock when it comes to including PHPUnit in my scripts. I know that I need to set the include path on PHP but every combination I've tried fails without the compiler seeing the PHPUnit_Framework_TestCase class.

    I just ran updates on PHP and PEAR and PHPUnit is installed on the computer because I can access it through the command line just fine.

    PHPUnit is installed at /usr/share/php/PHPunit

    Pear is at /usr/share/php/PEAR

    Is there something I'm missing? This is my first time trying to use PHPUnit or even something from PEAR for that matter. I'm on Ubuntu 10.10. Any help would be appreciated.

    Edit - There is nothing in the include path in my PHP ini. Right now the code is just

    <?php
    class Stacktest extends PHPUnit_Framework_TestCase
    {
    
    }
    

    I have no idea what to include or what to set in the include path because it seems that for all the info on the web about PHPUnit, this little bit of information is critically absent.

  • Jarrod Nettles
    Jarrod Nettles over 13 years
    In trying to run in the browser it properly loads TestRunner.php but fails to load the file of the parent class PHPUnit_Runner_BaseTestRunner. I get the following error when running this in the browser: Fatal error: Class 'PHPUnit_Runner_BaseTestRunner' not found in /usr/share/php/PHPUnit/TextUI/TestRunner.php on line 60
  • Poelinca Dorin
    Poelinca Dorin over 13 years
    first , how did you installed phpunit ? if pear then remove it and install it thru apt-get you whont have any problems with include files anymore !
  • Poelinca Dorin
    Poelinca Dorin over 13 years
    second if you get that error you can require that file too require_once 'PHPUnit/Runner/BaseTestRunner.php'; before TestRunner.php is required , but i have a strong fealing smth whent wrong with you're install so this might not be the last error you see , that's why i recomend using apt-get as you will be shure it works .
  • Jarrod Nettles
    Jarrod Nettles over 13 years
    This was the correct solution - the autoloader has to be explicitly included. Still have to make sure that you have include paths set to pear as well as phpunit.
  • B Seven
    B Seven over 12 years
    Where? At the top of each test file?
  • B Seven
    B Seven over 12 years
    After spending hours and hours trying to install PHPUnit, this worked for me.
  • Tyler Collier
    Tyler Collier over 11 years
    @BSeven, I'm not sure exactly how to help you, but this answer was helpful enough to get me going in my situation. I'm using PhpStorm 6, and I was able to go into Preferences (Project) > PHP > PHPUnit, and set the PHPUnit library to 'use custom loader' and then I set the path to be my project's vendor/autoload.php file.
  • Owen Beresford
    Owen Beresford over 10 years
    This is two years afterwards, @PoelincaDorin, but the ubuntu package for phpunit leads to Call to undefined method PHP_CodeCoverage_Filter::getInstance()
  • Poelinca Dorin
    Poelinca Dorin over 10 years
    @OwenBeresford what version of ubuntu and php are you using ? I haven't played with it for a long long time but i cold give it a shot if you want.
  • Owen Beresford
    Owen Beresford over 10 years
    @PoelincaDorin, PHP 5.4.17, PHPUnit 3.7.28, ubuntu 12.04 ~ will be grading that soon. Since i wrote that, I removed it via dpkg, and hacked the pear config, then added it via pear which solves the problem. Thanks anyway
  • Michiel van der Blonk
    Michiel van der Blonk over 9 years
    There is no need to 'locate' the correct file. Use "php -i" to get the full configuration. It contains the currently loaded configuration file path. Or use phpinfo() in a php file.
  • m3nda
    m3nda about 9 years
    php -i | grep php.ini grep is your friend, but, if you use php from Apache you should check from a browser adding php_info() and looking for "php.ini". Ubuntu files are under /etc/apache2/ and there are more files under/etc/php5/ {apache2|cli|cgi}.