【发布时间】:2022-01-16 03:37:57
【问题描述】:
引用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) {}
}
}
当我使用 PHPUnit 9.5.10(在 PHP 8.0.9 或 PHP 8.1.0 上)运行 vendor/bin/phpunit 时,我得到以下信息:
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') 对我不起作用。
有什么想法吗?
【问题讨论】:
-
这个问题和stackoverflow.com/q/70321515/569976 的区别在于另一个问题是关于 PHPUnit 9.4 和 PHPUnit 9.5 的。 PHPUnit 9.4 中的问题仅在 PHP 8.1 上表现出来,而这个问题至少在 PHP 8.0 和 8.1 中具有一致的行为。
标签: phpunit