为什么,致命错误:类“PHPUnit_Framework_TestCase”没有在...中找到?
为什么我会收到此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
对于那些在将 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
changelogchangelog ).You will need to refactor things like
\PHPUnit_Framework_TestCase
to\PHPUnit\Framework\TestCase
Update 2023-03-30: updated changelog link based on comment
PHPUnit 文档
说 曾经说过包含/需要 PHPUnit/Framework.php,如下所示:更新
从 PHPUnit 3.5 开始,有一个内置的自动加载器类可以为您处理这个问题:
感谢 Phoenix指出这一点!
The PHPUnit documentation
saysused to say to include/require PHPUnit/Framework.php, as follows:UPDATE
As of PHPUnit 3.5, there is a built-in autoloader class that will handle this for you:
Thanks to Phoenix for pointing this out!
对于更高版本的phpunit,例如6.4
您必须使用命名空间 PHPUnit\Framework\TestCase
使用 TestCase 而不是 PHPUnit_Framework_TestCase
For higher version of phpunit such as 6.4
You must use the namespace PHPUnit\Framework\TestCase
use TestCase instead PHPUnit_Framework_TestCase
我在 PHP5 上运行 PHPUnit 测试,然后,我还需要支持 PHP7。这就是我所做的:
在composer.json中:
在我的PHPUnit引导文件中(在我的例子中,
/tests/bootstrap.php
):换句话说,这适用于最初为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:
In my PHPUnit bootstrap file (in my case,
/tests/bootstrap.php
):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_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:
I submitted a crude PR to start conversation for correcting the documentation.
您只需安装 PHPUnit 即可运行命令 (https://github.com/sebastianbergmann/phpunit /#php-archive-phar):
运行单个测试
然后运行 PHPunit 测试:
测试文件内容如下:
运行测试套件
测试套件的配置:演示套件.xml。
demo
是包含所有测试的目录。测试文件必须命名为*_test.php
(后缀
)。测试套件使用以下命令运行:
You can simply install PHPUnit to run commands (https://github.com/sebastianbergmann/phpunit/#php-archive-phar):
Run single test
And then run PHPunit test:
Content of test file is following:
Run test suite
Configuration of test suite: demosuite.xml.
demo
is directory containing all tests. Test files must be named as*_test.php
(suffix
).Test suite runs with following commands:
假设:
操作:
在控制台中输入以下命令:
注释:
您不需要在新版本的 PHPUnit 中包含任何内容,除非您不想在控制台中运行。例如,在浏览器中运行测试。
Assumption:
Action:
Enter the following command in the console:
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.
我使用 ZF2 并在将 'PHPUnit_Framework_TestCase' 替换为 '\PHPUnit\Framework\TestCase' 时为我工作
I use ZF2 and work for me when replaced 'PHPUnit_Framework_TestCase' to '\PHPUnit\Framework\TestCase'
我让它
在我的测试功能的顶部工作。
I got it working with
at the top of my test function.
如果您有 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)
您很可能正在运行 WordPress 核心测试,并且最近将 PhpUnit 升级到版本 6。如果是这种情况,那么最近对 PhpUnit 中命名空间的更改将破坏您的代码。
幸运的是,有一个核心测试补丁 https://core.trac.wordpress.org/ changeset/40547 这将解决这个问题。它还包括对 travis.yml 的更改,您的设置中可能没有这些更改;如果是这种情况,那么您需要编辑 .diff 文件以忽略 Travis 补丁。
编辑补丁文件以删除补丁的 Travis 部分(如果不需要)。从文件顶部删除到此行的正上方:
将差异保存在 /includes/ 目录上方的目录中 - 在我的例子中,这是 Wordpress 目录本身
使用 Unix 修补工具来修补文件。您还需要删除前几个斜杠以从绝对目录结构移动到相对目录结构。正如您从上面的第 3 点看到的,包含目录之前有五个斜杠,-p5 标志将为您删除它。
完成此操作后,我的测试再次正确运行。
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.
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:
Save the diff in the directory above your /includes/ directory - in my case this was the Wordpress directory itself
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.
After I did this my tests ran correctly again.
注意:命令
php bin/consolegenerate:doctrine:crud
也会在src/Tests
中创建TestController
,这样它就可以如果您没有UnitTests
,当您尝试启动服务器时会抛出错误。删除文件修复它!NOTICE: Command
php bin/console generate:doctrine:crud
also createTestController
insrc/Tests
so it can throw error when you tried to start server if you don't haveUnitTests
. Remove the file fix it!对我来说,这是因为我运行
而不是
在工作目录中已经配置了 phpunit.xml 文件时运行。
For me, it was because I ran
instead of
when I already had a configured
phpunit.xml
file in the working directory.我在 window 10 上使用 php 5.6,并添加 zend 1.12 版本
前
有效。我们需要在 ControllerTestCase.php 文件中添加上面的语句
I am using php 5.6 on window 10 with zend 1.12 version for me adding
before
worked. We need to add this above statement in ControllerTestCase.php file
PHPUnit 与 Composer Autoloader
我正在使用 Composer,并发现自己正在解决这个问题。旧样式(
PHPUnit_Framekwork_TestCase
)或新样式(\PHPUnit\Framework\TestCase
)都不适合我。阅读完这里和其他地方的所有答案后,我发现它非常微不足道。注意: PHP 命名空间不区分大小写,但 Composer 区分大小写...
在上面的示例中,请注意
\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...
In the above example, note the change from
\PhpUnit\...
to\PHPUnit\...
.This makes all the difference with the composer autoloader.