Fatal error: Class 'PHPUnit_Framework_TestCase' not found

28,268

Solution 1

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.

Solution 2

For new PHPUnit versions you need to extend the TestCase only instead of PHPUnit_Framework_TestCase with the help of the use use PHPUnit\Framework\TestCase;

Old and Depricated Way:

// No namespace in this old method

class ClassATest extends PHPUnit_Framework_TestCase {

}

New Way adopted in Versions like 6.x:

// Namespace used in this new method

use PHPunit\Framework\TestCase;

class ClassATest extends TestCase {

}

If you are not using this new method you'll be getting the difficulties. Adopt it and this will fix your problem.

Solution 3

You haven't mentioned which version of PHP you are using, but if it is 3.5 or higher, just add

require_once 'PHPUnit/Autoload.php';

To the top of your code to pull in the required files.

If not,use:

require_once 'PHPUnit/Framework/TestCase.php';

It's unlikely that you're using a version earlier than 3.5, but I've included that just in case.

Solution 4

I think you just have to get the latest version of PHPUnit and not include any file as mentioned below. This worked for me when I upgraded to latest version but was running old Unit tests which had:

require_once 'PHPUnit/Framework/TestCase.php';

Removing this line fixed the problem.

Solution 5

For those arriving here after updating phpunit to version 6 released. You will need to rename the class like: \PHPUnit_Framework_TestCase to \PHPUnit\Framework\TestCase

Share:
28,268
user2118784
Author by

user2118784

Updated on February 06, 2020

Comments

  • user2118784
    user2118784 over 4 years

    I am using XAMPP 1.8.1, Windows 7 64 bit, Netbeans IDE 7.4 I have installed PHPUnit. When I run a code, I am getting the below error.

    Fatal error: Class 'PHPUnit_Framework_TestCase' not found in D:\xampp\htdocs\PHPUnit\index.php on line 6

    The Code is:

    <?php
    
    class StackTest extends PHPUnit_Framework_TestCase {
    
        public function testPushAndpop() {
            $stack = array();
            $this->assertEquals(0, count($stack));
    
            array_push($stack, 'foo');
            $this->assertEquals('foo', $stack[count($stack) - 1]);
            $this->assertEquals(1, count($stack));
    
            $this->assertEquals('foo', array_pop($stack));
            $this->assertEquals(0, count($stack));
        }
    
    }
    
    ?>
    

    I have added D:\xampp\php in environment variable PATH Anyone suggest me how to solve this problem? Thanks in advance.