简体   繁体   English

致命错误:找不到类'PHPUnit_Framework_TestCase'

[英]Fatal error: Class 'PHPUnit_Framework_TestCase' not found

I am using XAMPP 1.8.1, Windows 7 64 bit, Netbeans IDE 7.4 I have installed PHPUnit. 我正在使用XAMPP 1.8.1,Windows 7 64位,Netbeans IDE 7.4我已经安装了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 致命错误:第6行的D:\\ xampp \\ htdocs \\ PHPUnit \\ index.php中找不到类'PHPUnit_Framework_TestCase'

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? 我在环境变量PATH中添加了D:\\ xampp \\ php任何人都建议我如何解决这个问题? Thanks in advance. 提前致谢。

You haven't mentioned which version of PHP you are using, but if it is 3.5 or higher, just add 您还没有提到您使用的是哪个版本的PHP,但如果它是3.5或更高版本,则只需添加

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. 您不太可能使用3.5之前的版本,但我已经包含了以防万一。

Are you trying to run the test in a web browser? 您是否尝试在Web浏览器中运行测试? This confused me for a while. 这困惑了我一段时间。 PHPUnit tests are written to be run on the command line. 编写PHPUnit测试以在命令行上运行。

I think you just have to get the latest version of PHPUnit and not include any file as mentioned below. 我想你只需要获得最新版本的PHPUnit,而不包括下面提到的任何文件。 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; 对于新的PHPUnit版本,您需要在use PHPUnit\\Framework\\TestCase;的帮助下仅扩展TestCase而不是PHPUnit_Framework_TestCase 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: 在像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. 采用它,这将解决您的问题。

For those arriving here after updating phpunit to version 6 released. 对于那些在更新phpunit到版本6发布后到达这里的人。 You will need to rename the class like: \\PHPUnit_Framework_TestCase to \\PHPUnit\\Framework\\TestCase 您需要将类重命名为: \\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 第一个问题,对有些人,是,虽然类名是区分insentive在PHP中,磁带自动加载机有时是没有,所以你需要使用父类的正确名称: PHPUnit_Framework_TestCase

The second problem is an incompatibility of PHPUnit 6 with itself. 第二个问题是PHPUnit 6与其自身的不兼容性。 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). 与之前的建议相反,包括PHPUnit/Autoload.phpPHPUnit/Framework/TestCase.php在PHPUnit 4.2.6(Debian jessie)和6.5.5(Debian sid)中都被证明是不必要的。 It's also not necessary to write a phpunit.xml , bootstrap.php , etc. as included in similar advice. 也没有必要编写类似的建议中包含的phpunit.xmlbootstrap.php等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP 致命错误:找不到类“PHPUnit_Framework_TestCase” - PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found Symfony:致命错误:找不到类'PHPUnit_Framework_TestCase' - Symfony: Fatal error: Class 'PHPUnit_Framework_TestCase' not found in 为什么,致命错误:在...中找不到类“PHPUnit_Framework_TestCase”? - Why, Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...? PHPUnit CodeIgniter生成夹具PHP致命错误:未找到类“ PHPUnit_Framework_TestCase” - PHPUnit CodeIgniter Generate Fixtures PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found 致命错误:在第24行的dbunit \\ PHPUnit \\ Extensions \\ Database \\ TestCase.php中找不到类“ PHPUnit_Framework_TestCase” - Fatal error: Class 'PHPUnit_Framework_TestCase' not found in dbunit\PHPUnit\Extensions\Database\TestCase.php on line 24 PHPUnit类“错误”不会扩展PHPUnit_Framework_TestCase - PHPUnit Class “Error” does not extend PHPUnit_Framework_TestCase PHP 单元 - 找不到类“PHPUnit_Framework_TestCase” - PHP unit - Class 'PHPUnit_Framework_TestCase' not found 在Symfony2项目中找不到类&#39;PHPUnit_Framework_TestCase&#39; - Class 'PHPUnit_Framework_TestCase' not found in Symfony2 project 如何处理“ PHPUnit_Framework_TestCase”类未找到错误? - How do I deal with Class 'PHPUnit_Framework_TestCase' not found error? 找不到Symfony / PHPUnitClass&#39;PHPUnit_Framework_TestCase&#39; - Symfony/PHPUnitClass 'PHPUnit_Framework_TestCase' not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM