如何在 phpunit 测试期间查看完整的警告消息?

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

终端只告诉我

There was 1 Warning:
。但是我怎样才能看到完整的警告消息以及它是从哪里触发的呢?

--- 这是我的 phpunit.xml 文件

<?xml version="1.0"?>
<phpunit
        colors="true"
        verbose="true"
        stopOnFailure="false"
        convertErrorsToExceptions="true"
        convertNoticesToExceptions="true"
        convertWarningsToExceptions="true"
>
    <testsuites>
        <testsuite name="tests">
            <directory>tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

php phpunit vagrant simple-phpunit
2个回答
8
投票

添加@AndorDávid 评论作为答案,以便更容易消化:

使用此配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<PHPUnit
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
  xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"     
  displayDetailsOnTestsThatTriggerWarnings="true"
  colors="true">
  <!-- rest of the config file -->
</phpunit>

0
投票

希望值得一提的是另一种方法,如何在 PHPUnit 测试期间显示来自 PHP 的“警告”。

一种方法是通过在测试中使用

error_reporting(E_ALL)
setUp()
方法启用PHP错误报告来显示所有PHP错误:

公共函数setUp(): void
{
  # 开启错误报告
  错误报告(E_ALL);
  // ...
}

此后比较任一解决方案的冗长程度:

使用

error_reporting(E_ALL)
输出的第一个示例:


PHPUnit 11.0.3 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.0
Configuration: C:\...\tests\phpunit.xml

.
Warning: Undefined property: stdClass::$idc in C:\...\lib\MyClass.php on line 324
W                                                                  2 / 2 (100%)

Time: 00:01.221, Memory: 26.00 MB

第二种方法在其他答案中已经提到过,即在

displayDetailsOnTestsThatTriggerWarnings="true"
配置文件中使用
phpunit.xml
属性。

第二个输出示例

displayDetailsOnTestsThatTriggerWarnings="true"
:


PHPUnit 11.0.3 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.0
Configuration: C:\...\tests\phpunit.xml

.W                                                                  2 / 2 (100%)

Time: 00:01.204, Memory: 26.00 MB

1 test triggered 1 PHP warning:

1) C:\Users\...\lib\MyClass.php:324
Undefined property: stdClass::$foo

Triggered by:

* MyClassTest::test_MyClass_searchById
  C:\Users\...\tests\MyClassTest.php:414

OK, but there were issues!
Tests: 2, Assertions: 4, Warnings: 1.
© www.soinside.com 2019 - 2024. All rights reserved.