Quoting https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices , "默认情况下,PHPUnit 将测试执行期间触发的 PHP 错误、警告和通知转换为异常“。考虑到这一点,这是我的单元测试:
<?php
class DemoTest extends PHPUnit\Framework\TestCase
{
public function testDemo()
{
try {
trigger_error('zzz', E_USER_DEPRECATED);
} catch (\Throwable $e) {}
}
}
当我跑步时vendor/bin/phpunit
在 PHP 8.0.9 上使用 PHPUnit 9.4.0 我得到以下内容(输出的不相关部分已被删除):
PHPUnit 9.4.0 by Sebastian Bergmann and contributors.
R 1 / 1 (100%)
当我跑步时vendor/bin/phpunit
在 PHPUnit 9.4.0 和 PHP 8.1.0 上,我得到以下信息:
Deprecated: PHPUnit\Runner\DefaultTestResultCache implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in C:\path\to\code\vendor\phpunit\phpunit\src\Runner\DefaultTestResultCache.php on line 34
PHPUnit 9.4.0 by Sebastian Bergmann and contributors.
R 1 / 1 (100%)
IE。在 PHP 8.1 中,就像我得到了 Throwable 的完整转储,包括堆栈跟踪,而在 PHP 8.0 中我没有得到它。
有任何想法吗?我不想在输出中看到错误,如果没有抛出异常,那么$this->expectException('PHPUnit\\Framework\\Error\\Deprecated')
也不适合我