How to see full warning messages during phpunit test?
Asked Answered
S

2

15

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>

Siobhansion answered 15/5, 2021 at 8:7 Comment(3)
In the question the used PHPUnit version is 7.3.0 My answer is related to PHPUnit 10.x, but I still hope this will help somebody. <pre><code> <?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"> </phpunit> </code></pre>Aerodrome
Kudos to @AndorDávid! displayDetailsOnTestsThatTriggerWarnings="true" worked for me using PHPUnit 10.Disannul
@AndorDávid That should be an answerBab
E
15

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>
Estelaestele answered 29/9, 2023 at 18:1 Comment(0)
H
1

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.
Hadden answered 5/3 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.