Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error: Class 'PHPUnit_Framework_TestCase' not found

Tags:

php

phpunit

pear

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.

like image 774
user2118784 Avatar asked Feb 14 '14 08:02

user2118784


5 Answers

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.

like image 130
MKJ Avatar answered Nov 06 '22 12:11

MKJ


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.

like image 32
kguest Avatar answered Nov 06 '22 12:11

kguest


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.

like image 3
MikeiLL Avatar answered Nov 06 '22 12:11

MikeiLL


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.

like image 2
pdolinaj Avatar answered Nov 06 '22 12:11

pdolinaj


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

like image 1
Fabien Thetis Avatar answered Nov 06 '22 11:11

Fabien Thetis