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
使用 PHPUnit 9.5.10(在 PHP 8.0.9 或 PHP 8.1.0 上)我得到以下信息:
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.
R 1 / 1 (100%)
Deprecated: zzz in C:\path\to\code\tests\DemoTest.php on line 8
Time: 00:00.007, Memory: 6.00 MB
There was 1 risky test:
1) DemoTest::testDemo
This test did not perform any assertions
C:\path\to\code\tests\DemoTest.php:5
OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 0, Risky: 1.
我不想在输出中看到错误,如果没有抛出异常,那么$this->expectException('PHPUnit\\Framework\\Error\\Deprecated')
不会为我工作。
有任何想法吗?
In the 9.5.10 的变更日志 https://github.com/sebastianbergmann/phpunit/blob/master/ChangeLog-9.5.md#9510---2021-09-25据称:
默认情况下,PHPUnit 不再将 PHP 弃用转换为异常(配置convertDeprecationsToExceptions =“true”以启用此功能)
所以这是预料之中的。从技术上讲,人们甚至可以认为有关“错误、警告和通知”的声明是正确的,因为弃用是它们自己的类别。
您通常将其配置为XML配置文件 https://phpunit.readthedocs.io/en/9.5/configuration.html,提供有-c
.
另请注意文档页面上有关error_reporting
ini/运行时设置:它也不能排除弃用(E_ALL
很好)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)