Fatal error: Class 'PHPUnit_Framework_TestCase' not found

强颜欢笑 提交于 2019-12-05 18:42:39

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.

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 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.

Fabien Thetis

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!