Why, Fatal Error: Class 'Phpunit_Framework_Testcase' Not Found in ...

Why, Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...?

The PHPUnit documentation says used to say to include/require PHPUnit/Framework.php, as follows:

require_once ('PHPUnit/Framework/TestCase.php');

UPDATE

As of PHPUnit 3.5, there is a built-in autoloader class that will handle this for you:

require_once 'PHPUnit/Autoload.php';

Thanks to Phoenix for pointing this out!

Class 'PHPUnit_Framework_TestCase' not found

There is a difference between namespace structure between PHPUnit <6 and PHPUnit 6.

You may consider the following solution for backward compatibility:

// backward compatibility
if (!class_exists('\PHPUnit\Framework\TestCase') &&
class_exists('\PHPUnit_Framework_TestCase')) {
class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase');
}

The old PHPUnit versions use \PHPUnit_Framework_TestCase but the new one uses \PHPUnit\Framework\TestCase. With the backward compatibility applied you can use the class name that is compatible with the new version of PHPUnit (i.e. \PHPUnit\Framework\TestCase) and it is going to work also with older versions.

Update
In order to cover support for PHP 5.3 you have to remove a \ character before the alias class, i.e.

class_alias('\PHPUnit_Framework_TestCase', 'PHPUnit\Framework\TestCase');

Fatal error: Class 'PHPUnit_Framework_TestCase' not found

Are you trying to run the test in a web browser? This confused me for a while. PHPUnit tests are written to be run on the command line.

PHP unit - Class 'PHPUnit_Framework_TestCase' not found

You appear to have one, or both of two problems.

  • Not using Composer, This will also provide a vendor/autoload.php file, that will automatically pull in any files that have been included via the composer.json file.
  • The latest version of PHPunit (v>6.0) no longer uses the long class names. the still-supported v5+ does though.

The recommended mechanism to install PHPunit 5.7 is:

composer require --dev phpunit/phpunit:^5.7

Alternatively, it can be downloaded as a single phpunit.phar that can be run much as any other PHP script.



Related Topics



Leave a reply



Submit