当我尝试在Netbeans中运行PHPUnit时,我遇到了此错误:
Fatal error: Class 'PHPUnit_Framework_TestSuite' not found in C:UsersjulianAppDataRoamingNetBeans8.2phpunitNetBeansSuite.php on line 63
Done.
这在 Netbeans 和 CLI 中都会发生。
我通过导航到以下目录开始调试此问题:C:UsersjulianAppDataRoamingNetBeans8.2phpunit
.
该目录包含一个文件:NetBeansSuite.php
。我打开它寻找线索,看到了这一行:
class NetBeansSuite extends PHPUnit_Framework_TestSuite {
我没有看到任何具体的PHPUnit_Framework_TestSuite
类。
接下来是NetBeansSuite.php
文件没有任何可能包含PHPUnit_Framework_TestSuite
类的include
或require
语言结构。
因此,为了解决致命错误问题,我应该在NetbeansSuite.php
中包含PHPUnit_Framework_TestSuite
。
这是一个问题,因为我不是NetbeansSuite.php
的作者。 最重要的是,NetbeansSuite.php
的作者在评论部分写道:
<b>WARNING: User changes to this file should be avoided.</b>
我在评论中进一步阅读:
* Copyright 2010 Oracle and/or its affiliates. All rights reserved.
我想NetbeansSuite.php
文件已经过时了。
在网上搜索让我想到了这个堆栈溢出问题:
为什么,致命错误:在...中找不到类"PHPUnit_Framework_TestCase"?
他们声称使用PHPUnit_Framework_TestCase的命名空间版本应该可以解决问题。所以我按照他们写的做了。
我固执地改变了NetBeansSuite.php
. 旧代码:
class NetBeansSuite extends PHPUnit_Framework_TestSuite {
新代码:
require_once 'PHPUnit/Autoload.php';
use PHPUnitFrameworkTestCase;
class NetBeansSuite extends TestCase {
我尝试再次运行测试用例,这是不幸的结果:
Fatal error: Declaration of CalculatorTest::setUp() must be compatible with PHPUnitFrameworkTestCase::setUp(): void in C:wamp64wwwtestCalculatorTest.php on line 97
换句话说,它给了我一个新的问题。
我的系统:
Windows 10
WAMP
php 7.2.14
PHPUnit 8.0.6
Netbeans version 8.2 (Netbeans PHPUnit plugin installed through Tools > Plugins. Version: 0.26.2)
我的问题是:有谁知道 NetBeansSuite.php 文件在上述系统中应该是什么样子的?
我的测试用例中的设置方法:
protected function setUp()
{
$this->object = new Calculator;
}
更新:骨架生成器项目被放弃。 见链接: https://github.com/sebastianbergmann/phpunit-skeleton-generator 因此,为了修复
Declaration of CalculatorTest::setUp() must be compatible with PHPUnitFrameworkTestCase::setUp()
错误,应在测试用例中使用相应的返回类型声明。
// I added : void
protected function setUp(): void
{
$this->object = new Calculator;
}
// : void needs to be added in the tearDown method as well
protected function tearDown(): void
不幸的是,这给了我新的问题:
Warning: require_once(PHPUnit/Autoload.php): failed to open stream: No such file or directory in C:wamp64wwwtestCalculatorTest.php on line 4
Fatal error: require_once(): Failed opening required 'PHPUnit/Autoload.php' (include_path='.;C:phppear') in C:wamp64wwwtestCalculatorTest.php on line 4
我通过手动安装 PEAR 并在C:phpPEAR
中创建一个新的"PHPUnit"目录来解决此问题。然后我创建了一个新的Autoload.php
文件。我用一个PSR-4
示例文件填充了Autoload.php
的内容:https://www.php-fig.org/psr/psr-4/。
这解决了自动加载问题,但是在执行测试用例期间遇到了一个新问题。
C:wamp64wwwtest>phpunit CalculatorTest.php
PHPUnit 8.0.6 by Sebastian Bergmann and contributors.
Time: 170 ms, Memory: 10.00 MB
No tests executed!
它显示了No tests executed!
但我有 5 个测试。 我会为此提出一个新问题。 这是:通过PHAR安装在Windows上的PHPUnit 8显示未执行任何测试
最初,您确实应该更新命名空间,因为它已在最新版本的 PHPUnit 中进行了更改。
但是,该错误指示您的CalculatorTest::setUp()
方法与 PHPUnit 版本不兼容。你能在这里发布这种方法吗?
对于同样的问题,我直接更改了文件"NetBeansSuite.php"(C:\Users\USERNAME\AppData\Roaming\NetBeans\8.2\phpunit),如下所示:
class NetBeansSuite extends PHPUnitFrameworkTestSuite {
在那之后,CLI 和 Netbeans 开始工作了。