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.
<?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> – AerodromedisplayDetailsOnTestsThatTriggerWarnings="true"
worked for me using PHPUnit 10. – Disannul