如何让 PHPUnit 解释弃用和警告?

问题描述 投票:0回答:3

我刚刚学习如何使用 PHPUnit。我从命令行运行它,在结果中我得到

OK, but there are issues! Tests: 5, Assertions: 15, Warnings: 2, Deprecations: 5.

我不知道如何让它解释导致弃用和警告计数的原因。 如何找出测试代码中导致此问题的原因?我已尝试添加标志

--display-deprecations
,但它没有改变任何内容。

我正在使用标志

--testdox
--stderr
-v
,但它们没有帮助。我已经尝试过
--debug
--verbose
但它说它们都不受支持的标志。

我如何知道我的代码中存在什么问题?趁着这个机会,我不妨问问大家。这是我的测试代码:

final class FetchForErrorsTest extends TestCase
{
    /**
     * @dataProvider urlsForBasicTest
     */
    public function testNoErrors($url)
    {
        $rr = new RequestResponse(TestConf::$rootUrl.'/'.$url);
        $body = $rr->get();
        $this->assertStringEndsWith('</html>', rtrim($body));
        $this->assertEquals($rr->status, 200);
    }
    public static function urlsForBasicTest()
    {
        return [
            ['index.php'],
            ...
        ];
    }
}

我还收到以下内容的弃用和警告警报,但没有任何进一步的解释:

final class DIContainerTest extends TestCase
{
    public static $di;
    public static $foo;
    public static function setUpBeforeClass(): void
    {
        self::$di = new DIContainer();
        self::$di->register(
            ["Bar", "BarInterface"], 
            "Bar", 'singleton'
        );
        self::$di->register(
            ["Foo", "FooInterface"],
            "Foo"
        );
    }
    public function testCalledFactory()
    {
        self::$foo = self::$di->call("FooInterface::make");
        $this->assertEquals(get_class(self::$foo), "Foo");
    }
    public function testRecursivelyInstantatedConcretes()
    {
        self::$foo = self::$di->call("FooInterface::make");
        $this->assertEquals(get_class(self::$foo->bar), "Bar");
    }
}

如果能找到一种方法来更明确地说明弃用和警告的内容,那就太好了。

谢谢

phpunit deprecated deprecation-warning
3个回答
4
投票

您需要配置 phpunit.xml 文件来自定义它如何处理警告、弃用等。这通常位于项目的根目录中。

它看起来像这样,有多种方法可以配置它,我只是在这里选择了一些选项,请检查文档https://docs.phpunit.de/en/10.1/configuration.html以了解所有选项。

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         displayDetailsOnTestsThatTriggerDeprecations="true"
         displayDetailsOnTestsThatTriggerErrors="true"
         displayDetailsOnTestsThatTriggerNotices="true"
         displayDetailsOnTestsThatTriggerWarnings="true"
         strict="true"
         >
</phpunit>

0
投票

如果您尚未使用它运行 php,则可以将此指令放入您的

php.ini
(或者您可以使用以下指令运行 PHPUnit 脚本):
error_reporting  = E_ALL

您现在可以在

php
日志中获取弃用信息


0
投票

https://docs.phpunit.de/en/10.1/configuration.html#the-displaydetailsonteststhattriggererrors-attribute

添加这些值(特别是 testdox="false" ),然后它似乎显示实际消息而不是 D

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    testdox="false" 
    displayDetailsOnIncompleteTests="true"
    displayDetailsOnSkippedTests="true"
    displayDetailsOnTestsThatTriggerDeprecations="true"
    displayDetailsOnTestsThatTriggerErrors="true"
    displayDetailsOnTestsThatTriggerNotices="true"
    displayDetailsOnTestsThatTriggerWarnings="true"
>

但输出不如以前那么漂亮..但至少我现在可以看到它们..

© www.soinside.com 2019 - 2024. All rights reserved.