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

发布于 2024-11-08 17:31:54 字数 114 浏览 6 评论 0原文

为什么我会收到此 PHP 错误?

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

Why I'm getting this PHP error?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

甜是你 2024-11-15 17:31:54

对于那些在将 phpunit 更新到 2017 年 2 月 3 日发布的版本 6 或更高版本(例如使用 Composer)后到达此处的用户,您可能会收到此错误,因为 phpunit 代码现在已命名空间(检查 < a href="https://github.com/sebastianbergmann/phpunit/blob/master/ChangeLog-6.0.md#changed-1" rel="noreferrer">变更日志 变更日志 )。

您需要将 \PHPUnit_Framework_TestCase 重构为 \PHPUnit\Framework\TestCase

更新 2023-03-30:根据评论更新变更日志链接

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

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

Update 2023-03-30: updated changelog link based on comment

缱绻入梦 2024-11-15 17:31:54

PHPUnit 文档 说 曾经说过包含/需要 PHPUnit/Framework.php,如下所示:

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

更新

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

require_once 'PHPUnit/Autoload.php';

感谢 Phoenix指出这一点!

The PHPUnit documentation says used to say to include/require PHPUnit/Framework.php, as follows:

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:

require_once 'PHPUnit/Autoload.php';

Thanks to Phoenix for pointing this out!

逆流 2024-11-15 17:31:54

对于更高版本的phpunit,例如6.4
您必须使用命名空间 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 {

}

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

use TestCase instead PHPUnit_Framework_TestCase

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

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

}
梦情居士 2024-11-15 17:31:54

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

在composer.json中:

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

在我的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');

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

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

In composer.json:

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

In my PHPUnit bootstrap file (in my case, /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.

不知所踪 2024-11-15 17:31:54

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

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

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

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:

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

I submitted a crude PR to start conversation for correcting the documentation.

手心的温暖 2024-11-15 17:31:54

您只需安装 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

运行单个测试

然后运行 ​​PHPunit 测试:

phpunit test.php

测试文件内容如下:

<?php

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

    public function testSave()
    {

    }
}

运行测试套件

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

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

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

phpunit -c demosuite.xml --testsuite DemoTestSuite

You can simply install PHPUnit to run commands (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 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. demo is directory containing all tests. Test files must be named as *_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
寂寞笑我太脆弱 2024-11-15 17:31:54

假设:

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

操作:

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

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

注释:

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

Assumption:

Phpunit (3.7) is available in the console environment.

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.

埋情葬爱 2024-11-15 17:31:54

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

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

悲喜皆因你 2024-11-15 17:31:54

我让它

include("vendor/autoload.php");

在我的测试功能的顶部工作。

I got it working with

include("vendor/autoload.php");

at the top of my test function.

鹿童谣 2024-11-15 17:31:54

如果您有 Centos 或其他 Linux 发行版,则必须安装 phpunit 软件包,我使用 yum install phpunit 进行了安装,并且成功了。也许你必须添加一个存储库,但我认为它必须与默认存储库一起顺利工作(我有 CentOS 7)

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)

墨离汐 2024-11-15 17:31:54

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

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

  1. https://core.trac.wordpress.org 底部下载“Unified Diff”补丁/changeset/40547
  2. 编辑补丁文件以删除补丁的 Travis 部分(如果不需要)。从文件顶部删除到此行的正上方:

    索引:/branches/4.7/tests/phpunit/includes/bootstrap.php
    
  3. 将差异保存在 /includes/ 目录上方的目录中 - 在我的例子中,这是 Wordpress 目录本身

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

    $ cd [WORDPRESS 目录]
    $补丁-p5 <变更集_40547.diff 
    

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

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.

Fortunately, there's a patch to the core tests at https://core.trac.wordpress.org/changeset/40547 which 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.

  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 
    

After I did this my tests ran correctly again.

天冷不及心凉 2024-11-15 17:31:54

注意:命令php bin/consolegenerate:doctrine:crud也会在src/Tests中创建TestController,这样它就可以如果您没有 UnitTests,当您尝试启动服务器时会抛出错误。删除文件修复它!

NOTICE: Command php bin/console generate:doctrine:crud also create TestController in src/Tests so it can throw error when you tried to start server if you don't have UnitTests. Remove the file fix it!

时光倒影 2024-11-15 17:31:54

对我来说,这是因为我运行

$ phpunit .

而不是

$ phpunit

在工作目录中已经配置了 phpunit.xml 文件时运行。

For me, it was because I ran

$ phpunit .

instead of

$ phpunit

when I already had a configured phpunit.xml file in the working directory.

天赋异禀 2024-11-15 17:31:54

我在 window 10 上使用 php 5.6,并添加 zend 1.12 版本

require_once 'PHPUnit/Autoload.php';

抽象类 Zend_Test_PHPUnit_ControllerTestCase 扩展
PHPUnit_Framework_TestCase

有效。我们需要在 ControllerTestCase.php 文件中添加上面的语句

I am using php 5.6 on window 10 with zend 1.12 version for me adding

require_once 'PHPUnit/Autoload.php';

before

abstract class Zend_Test_PHPUnit_ControllerTestCase extends
PHPUnit_Framework_TestCase

worked. We need to add this above statement in ControllerTestCase.php file

单身情人 2024-11-15 17:31:54

PHPUnit 与 Composer Autoloader

我正在使用 Composer,并发现自己正在解决这个问题。旧样式(PHPUnit_Framekwork_TestCase)或新样式(\PHPUnit\Framework\TestCase)都不适合我。阅读完这里和其他地方的所有答案后,我发现它非常微不足道。

注意: PHP 命名空间不区分大小写,但 Composer 区分大小写...

// WRONG
class TestChild extends \PhpUnit\Framework\TestCase {

// RIGHT
class TestChild extends \PHPUnit\Framework\TestCase {

在上面的示例中,请注意 \PhpUnit\...< /code> 到 \PHPUnit\...
这使得 Composer 自动加载器变得与众不同。

PHPUnit With Composer Autoloader

I am using composer and found myself battling through this issue. Neither the old style (PHPUnit_Framekwork_TestCase) or the new style (\PHPUnit\Framework\TestCase) worked for me. After reading through all the answers here and elsewhere, I found it to be quite trivial.

NOTE: PHP namespaces are case insensitive, however composer is case-sensitive...

// WRONG
class TestChild extends \PhpUnit\Framework\TestCase {

// RIGHT
class TestChild extends \PHPUnit\Framework\TestCase {

In the above example, note the change from \PhpUnit\... to \PHPUnit\....
This makes all the difference with the composer autoloader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文