codeception / assert-throws
断言抛出异常而不停止测试
1.4.0
2024-02-06 21:28 UTC
Requires
- phpunit/phpunit: ^8.0|^9.0|^10.0|^11.0
README
在测试中处理异常而不停止!与 PHPUnit 和 Codeception 兼容。
安装
composer require "codeception/assert-throws" --dev
将 AssertThrows
特性包含到 TestCase 中
<?php class MyTest extends PHPUnit\Framework\TestCase { use Codeception\AssertThrows; //... }
用法
捕获代码块内抛出的异常。
<?php $this->assertThrows(NotFoundException::class, function() { $this->userController->show(99); }); // alternatively $this->assertThrows(new NotFoundException(), function() { $this->userController->show(99); }); // you can also assert that an exception is not throw $this->assertDoesNotThrow(NotFoundException::class, function() { $this->userController->show(99); });
您可以可选地测试异常消息
<?php $this->assertThrowsWithMessage( NotFoundException::class, 'my error message', function() { throw new NotFoundException('my error message'); } );
许可协议
Codeception AssertThrows
是开源软件,使用 MIT 许可协议。
© Codeception PHP 测试框架