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.
Fatal error: Class 'PHPUnit_Framework_TestCase' not found
Question
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.
Solution 3
OTHER TIPS
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.
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.
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.
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
There are two levels of answers to this.
The first problem, for some, is that, while class names are case-insentive in PHP, the autoloader sometimes is not, so you will need to use the correct name of the parent class: PHPUnit_Framework_TestCase
The second problem is an incompatibility of PHPUnit 6 with itself. If you, like many others, need to be able to write testcases that work with either version, create a new include file with the following content:
<?php
if (!class_exists('PHPUnit_Framework_TestCase') &&
class_exists('\\PHPUnit\\Framework\\TestCase')) {
abstract class PHPUnit_Framework_TestCase extends \PHPUnit\Framework\TestCase {
}
}
Then, include this file at the top of each testcase. The testcases themselves continue to inherit from the aforementioned class, for example, in code of mine:
class Minijson_Tests extends PHPUnit_Framework_TestCase {
Contrary to earlier advice, including PHPUnit/Autoload.php
or PHPUnit/Framework/TestCase.php
has proven to be unnecessary with both PHPUnit 4.2.6 (Debian jessie) and 6.5.5 (Debian sid). It’s also not necessary to write a phpunit.xml
, bootstrap.php
, etc. as included in similar advice.