php 致命错误:找不到类“PHPUnit_Framework_TestCase”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21773872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:26:06  来源:igfitidea点击:

Fatal error: Class 'PHPUnit_Framework_TestCase' not found

phpphpunitpear

提问by user2118784

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.

我使用的是 XAMPP 1.8.1、Windows 7 64 位、Netbeans IDE 7.4 我已经安装了 PHPUnit。当我运行代码时,出现以下错误。

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\phpin environment variable PATH Anyone suggest me how to solve this problem? Thanks in advance.

我在环境变量 PATH 中添加了D:\xampp\php有人建议我如何解决这个问题吗?提前致谢。

回答by kguest

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,但为了以防万一,我已将其包含在内。

回答by MikeiLL

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.

您是否尝试在 Web 浏览器中运行测试?这让我困惑了一段时间。PHPUnit 测试被编写为在命令行上运行。

回答by MKJ

For new PHPUnit versions you need to extend the TestCaseonly instead of PHPUnit_Framework_TestCasewith the help of the use use PHPUnit\Framework\TestCase;

对于新的 PHPUnit 版本,您需要扩展TestCaseonly 而不是在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.

如果您不使用这种新方法,就会遇到困难。采用它,这将解决您的问题。

回答by pdolinaj

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:

我认为您只需要获取最新版本的 PHPUnit,并且不包含下面提到的任何文件。当我升级到最新版本但正在运行具有以下功能的旧单元测试时,这对我有用:

require_once 'PHPUnit/Framework/TestCase.php';

Removing this line fixed the problem.

删除此行解决了问题。

回答by Fabien Thetis

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

对于那些在将 phpunit 更新到版本 6 后到达这里的人。您将需要重命名类,如: \PHPUnit_Framework_TestCase\PHPUnit\Framework\TestCase

回答by mirabilos

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 willneed 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 6with 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:

第二个问题是PHPUnit 6与自身不兼容。如果您和其他许多人一样,需要能够编写适用于任一版本的测试用例,请创建一个包含以下内容的新包含文件:

<?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.phpor PHPUnit/Framework/TestCase.phphas 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.

与早先的建议相反,包括PHPUnit/Autoload.phpPHPUnit/Framework/TestCase.php已证明对 PHPUnit 4.2.6 (Debian jessie) 和 6.5.5 (Debian sid) 都是不必要的。它也没有必要写phpunit.xmlbootstrap.php等如列入类似的建议。