php 为什么,致命错误:在...中找不到类“PHPUnit_Framework_TestCase”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6065730/
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 23:16:08  来源:igfitidea点击:

Why, Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...?

phpfatal-errorclassnotfoundexception

提问by Alvin

Why I'm getting this PHP error?

为什么我会收到这个 PHP 错误?

Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...

采纳答案by defines

The PHPUnit documentationsaysused to say to include/require PHPUnit/Framework.php, as follows:

PHPUnit文档过去常说,包括/需要的PHPUnit / Framework.php,如下所示:

require_once ('PHPUnit/Framework/TestCase.php');

UPDATE

更新

As of PHPUnit 3.5, there is a built-in autoloader class that will handle this for you:

从 PHPUnit 3.5 开始,有一个内置的自动加载器类可以为您处理这个问题:

require_once 'PHPUnit/Autoload.php';

Thanks to Phoenix for pointing this out!

感谢凤凰指出这一点!

回答by shadi

For those arriving here after updating phpunit to version 6 or greaterreleased on 2017-02-03 (e.g. with composer), you may be getting this error because phpunit code is now namespaced (check changelog).

对于那些在将 phpunit 更新到 2017 年 2月 3 日发布的版本 6 或更高版本(例如使用 composer)之后到达这里的人,您可能会收到此错误,因为 phpunit 代码现在已命名空间(检查更改日志)。

You will need to refactor things like \PHPUnit_Framework_TestCaseto \PHPUnit\Framework\TestCase

您将需要重构之类的东西\PHPUnit_Framework_TestCase\PHPUnit\Framework\TestCase

回答by Jijesh Cherrai

For higher versionof phpunit such as 6.4You must use the namespace PHPUnit\Framework\TestCase

对于更高版本的 phpunit 如6.4必须使用命名空间PHPUnit\Framework\TestCase

use TestCaseinstead PHPUnit_Framework_TestCase

使用TestCase代替PHPUnit_Framework_TestCase

// use the following namespace
use PHPUnit\Framework\TestCase;

// extend using TestCase instead PHPUnit_Framework_TestCase
class SampleTest extends TestCase {

}

回答by Bevan

You may get this error because you namespaced the file. If so you will need to specify that PHPUnit_Framework_TestCase is in the global namespace by preceding it with a backslash:

您可能会收到此错误,因为您对文件进行了命名空间。如果是这样,您需要通过在其前面加上反斜杠来指定 PHPUnit_Framework_TestCase 在全局命名空间中:

namespace AcmeInc\MyApplication\Tests
class StackTest extends \PHPUnit_Framework_TestCase {}

I submitted a crude PRto start conversation for correcting the documentation.

我提交了一份粗略的 PR以开始对话以纠正文档

回答by Nino ?kopac

I was running PHPUnit tests on PHP5, and then, I needed to support PHP7 as well. This is what I did:

我在 PHP5 上运行 PHPUnit 测试,然后,我也需要支持 PHP7。这就是我所做的:

In composer.json:

在 composer.json 中:

"phpunit/phpunit": "~4.8|~5.7"

In my PHPUnit bootstrap file (in my case, /tests/bootstrap.php):

在我的 PHPUnit 引导文件中(在我的例子中,/tests/bootstrap.php):

// PHPUnit 6 introduced a breaking change that
// removed PHPUnit_Framework_TestCase as a base class,
// and replaced it with \PHPUnit\Framework\TestCase
if (!class_exists('\PHPUnit_Framework_TestCase') && class_exists('\PHPUnit\Framework\TestCase'))
    class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');

In other words, this will work for tests written originally for PHPUnit 4 or 5, but then needed to work on PHPUnit 6 as well.

换句话说,这适用于最初为 PHPUnit 4 或 5 编写的测试,但随后也需要在 PHPUnit 6 上工作。

回答by radeklos

You can simply install PHPUnit to run commands (https://github.com/sebastianbergmann/phpunit/#php-archive-phar):

您可以简单地安装 PHPUnit 来运行命令(https://github.com/sebastianbergmann/phpunit/#php-archive-phar):

wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit

Run single test

运行单个测试

And then run PHPunit test:

然后运行 ​​PHPunit 测试:

phpunit test.php

Content of test file is following:

测试文件内容如下:

<?php

class StackTest extends PHPUnit_Framework_TestCase
{
    protected function setUp()
    {
    }

    public function testSave()
    {

    }
}

Run test suite

运行测试套件

Configuration of test suite: demosuite.xml. demois directory containing all tests. Test files must be named as *_test.php(suffix).

测试套件的配置:demosuite.xml。demo是包含所有测试的目录。测试文件必须命名为*_test.php( suffix)。

<testsuites>
    <testsuite name="DemoTestSuite">
        <directory suffix="test.php">demo</directory>
    </testsuite>
</testsuites>

Test suite runs with following commands:

测试套件使用以下命令运行:

phpunit -c demosuite.xml --testsuite DemoTestSuite

回答by svirk

Assumption:

假设:

Phpunit (3.7)is available in the console environment.

Phpunit (3.7)在控制台环境中可用。

Action:

行动:

Enter the following command in the console:

在控制台中输入以下命令:

SHELL> phpunit "{{PATH TO THE FILE}}"

Comments:

注释:

You do not need to include anything in the new versions of PHPUnit unless you do not want to run in the console. For example, running tests in the browser.

除非您不想在控制台中运行,否则您不需要在新版本的 PHPUnit 中包含任何内容。例如,在浏览器中运行测试。

回答by Ricardo Herrero

If you have Centos or other Linux distribution you have to install phpunit package, I did that with yum install phpunit and it worked. Maybe you can have to add a repository, but I think it has to work smooth with the default ones (I have CentOS 7)

如果你有 Centos 或其他 Linux 发行版,你必须安装 phpunit 包,我用 yum install phpunit 做到了这一点,它工作正常。也许您必须添加一个存储库,但我认为它必须与默认存储库配合使用(我有 CentOS 7)

回答by piersb

It may well be that you're running WordPress core tests, and have recently upgraded your PhpUnit to version 6. If that's the case, then the recent change to namespacing in PhpUnit will have broken your code.

很可能您正在运行 WordPress 核心测试,并且最近将您的 PhpUnit 升级到第 6 版。如果是这种情况,那么最近对 PhpUnit 中命名空间的更改将破坏您的代码。

Fortunately, there's a patch to the core tests at https://core.trac.wordpress.org/changeset/40547which will work around the problem. It also includes changes to travis.yml, which you may not have in your setup; if that's the case then you'll need to edit the .diff file to ignore the Travis patch.

幸运的是,https://core.trac.wordpress.org/changeset/40547 上有一个核心测试补丁可以解决这个问题。它还包括对 travis.yml 的更改,您的设置中可能没有这些更改;如果是这种情况,那么您需要编辑 .diff 文件以忽略 Travis 补丁。

  1. Download the "Unified Diff" patch from the bottom of https://core.trac.wordpress.org/changeset/40547
  2. Edit the patch file to remove the Travis part of the patch if you don't need that. Delete from the top of the file to just above this line:

    Index: /branches/4.7/tests/phpunit/includes/bootstrap.php
    
  3. Save the diff in the directory above your /includes/ directory - in my case this was the Wordpress directory itself

  4. Use the Unix patch tool to patch the files. You'll also need to strip the first few slashes to move from an absolute to a relative directory structure. As you can see from point 3 above, there are five slashes before the include directory, which a -p5 flag will get rid of for you.

    $ cd [WORDPRESS DIRECTORY]
    $ patch -p5 < changeset_40547.diff 
    
  1. https://core.trac.wordpress.org/changeset/40547底部下载“Unified Diff”补丁
  2. 如果不需要,请编辑补丁文件以删除补丁的 Travis 部分。从文件顶部删除到这一行的正上方:

    Index: /branches/4.7/tests/phpunit/includes/bootstrap.php
    
  3. 将差异保存在 /includes/ 目录上方的目录中 - 在我的情况下,这是 Wordpress 目录本身

  4. 使用 Unix 补丁工具修补文件。您还需要去除前几个斜杠以从绝对目录结构移动到相对目录结构。从上面的第 3 点可以看出,包含目录之前有五个斜杠, -p5 标志将为您删除。

    $ cd [WORDPRESS DIRECTORY]
    $ patch -p5 < changeset_40547.diff 
    

After I did this my tests ran correctly again.

完成此操作后,我的测试再次正确运行。

回答by Alysson Vicu?a de Oliveira

I use ZF2 and work for me when replaced 'PHPUnit_Framework_TestCase' to '\PHPUnit\Framework\TestCase'

我使用 ZF2 并在将 'PHPUnit_Framework_TestCase' 替换为 '\PHPUnit\Framework\TestCase' 时为我工作