【问题标题】:PHPUnit Plugin in Netbeans 8.2 gives Fatal error: Class 'PHPUnit_Framework_TestSuite' not found when trying to run a test caseNetbeans 8.2 中的 PHPUnit 插件给出致命错误:尝试运行测试用例时找不到类“PHPUnit_Framework_TestSuite”
【发布时间】: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 类的includerequire 语言结构。

所以,为了解决致命错误问题,我应该在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


【解决方案1】:

对于同样的问题,我直接将文件“NetBeansSuite.php”(C:\Users\USERNAME\AppData\Roaming\NetBeans\8.2\phpunit)更改为如下:

class NetBeansSuite extends \PHPUnit\Framework\TestSuite {

在那之后,CLI 和 Netbeans 开始工作了。

【讨论】:

    【解决方案2】:

    最初确实应该更新命名空间,因为它在 PHPUnit 的较新版本中已更改。

    但是,该错误表明您的 CalculatorTest::setUp() 方法与 PHPUnit 版本不兼容。你能把这个方法贴在这里吗?

    【讨论】:

    • setUp 方法如下所示:protected function setUp() { $this-&gt;object = new Calculator; }。没有真正的火箭科学。骨架生成器实际上是写的。也许它与静态/非静态有关??
    • @Julian 实际上对于更高版本的 PHPUnit 看起来不错。但是,该方法在 PHPUnit\Framework\TestCase::setUp 中看起来如何?另外你是如何安装 PHPUnit 的?因为据我所知,它是使用 netbeans 插件?如果是的话会是哪个版本
    • 我刚刚将它安装为 PHAR,使用此链接:phpunit.de/getting-started/phpunit-8.html 和此:phpunit.de/manual/6.5/en/…
    • Netbeans 中的 PHPUnit 插件版本:0.26.2。
    • 我解决了这个问题。 PHPUnit 插件版本 0.26.2 似乎已经过时了。因此 Skeleton Generator 也提供了一个过时的测试用例。所以,我只是在入门页面上使用了这个例子,它就奏效了。入门页面链接:phpunit.de/getting-started/phpunit-8.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2016-03-23
    • 1970-01-01
    • 2017-06-08
    • 2013-12-23
    • 2021-08-23
    • 1970-01-01
    相关资源
    最近更新 更多