How to see full warning messages during phpunit test?

3k views Asked by At

The terminal only tells me There was 1 Warning:. But how can I see the full warning message and where it's being triggered from? Screenshot of terminal

--- this is my phpunit.xml file

<?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>

2

There are 2 answers

0
ino On

Hope it is worthy to mention another approach how to show "warnings" that came from PHP during PHPUnit testing.

One way is to show all PHP Errors by enabling PHP error reporing using error_reporting(E_ALL) setUp() method in your tests:

public function setUp(): void
{
  # Turn on error reporting
  error_reporting(E_ALL);
  // ...
}

Hereafter compare verbosity of either solutions:

First example of output with 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

Second way was already mentioned in other answers is using displayDetailsOnTestsThatTriggerWarnings="true" attribute in phpunit.xml config file.

Second example of output with 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.
0
jcubic On

Adding @AndorDávid comment as the the answer so it's easier to digest:

Use this config file:

<?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>