【发布时间】:2019-08-18 05:42:27
【问题描述】:
当我尝试在 Netbeans 中运行 PHPUnit 时,我遇到了这个错误:
Fatal error: Class 'PHPUnit_Framework_TestSuite' not found in C:\Users\julian\AppData\Roaming\NetBeans\8.2\phpunit\NetBeansSuite.php on line 63
Done.
这在 Netbeans 和 CLI 中都会发生。
我通过导航到以下目录开始调试此问题:C:\Users\julian\AppData\Roaming\NetBeans\8.2\phpunit\。
该目录包含一个文件: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>
我在 cmets 中进一步阅读:
* Copyright 2010 Oracle and/or its affiliates. All rights reserved.
我猜NetbeansSuite.php 文件已经过时了。
在网上搜索让我想到了这个 stackoverflow 问题:
Why, Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...?
他们声称使用命名空间版本的 PHPUnit_Framework_TestCase 应该可以解决问题。所以我做了他们写的。
我顽固地换了NetBeansSuite.php。
旧代码:
class NetBeansSuite extends PHPUnit_Framework_TestSuite {
新代码:
require_once 'PHPUnit/Autoload.php';
use PHPUnit\Framework\TestCase;
class NetBeansSuite extends TestCase {
我尝试再次运行测试用例,结果很不幸:
Fatal error: Declaration of CalculatorTest::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp(): void in C:\wamp64\www\test\CalculatorTest.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 文件在上述系统中应该是什么样的吗?
我的测试用例中的 setUp 方法:
protected function setUp()
{
$this->object = new Calculator;
}
更新:骨架生成器项目已被放弃。 见链接:https://github.com/sebastianbergmann/phpunit-skeleton-generator 因此,为了修复
Declaration of CalculatorTest::setUp() must be compatible with PHPUnit\Framework\TestCase::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:\wamp64\www\test\CalculatorTest.php on line 4
Fatal error: require_once(): Failed opening required 'PHPUnit/Autoload.php' (include_path='.;C:\php\pear') in C:\wamp64\www\test\CalculatorTest.php on line 4
我通过手动安装 PEAR 并在 C:\php\PEAR 中创建一个新的“PHPUnit”目录解决了这个问题。然后我创建了一个新的Autoload.php 文件。我用PSR-4 的示例文件填充了Autoload.php 的内容:https://www.php-fig.org/psr/psr-4/。
这解决了 Autoload 问题,但我在执行测试用例时遇到了一个新问题。
C:\wamp64\www\test>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 个测试。
我会为此提出一个新问题。
这里是:PHPUnit 8 installed on Windows through PHAR shows No tests are executed
【问题讨论】:
-
你能添加你的测试设置方法吗?由于它似乎不兼容
标签: php netbeans-8 netbeans-plugins phpunit