codeception/assert-throws

断言抛出异常而不停止测试

1.4.0 2024-02-06 21:28 UTC

This package is auto-updated.

Last update: 2024-09-06 22:50:34 UTC


README

在测试中处理异常而不停止!与 PHPUnit 和 Codeception 兼容。

Actions Status Latest Stable Version Total Downloads License

安装

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 测试框架