codeception / assert-throws
Assert exception was thrown without stopping a test
Installs: 1 200 208
Dependents: 39
Suggesters: 0
Security: 0
Stars: 19
Watchers: 8
Forks: 10
Open Issues: 1
Requires
- phpunit/phpunit: ^8.0|^9.0|^10.0|^11.0
README
Handle exceptions inside a test without a stop! Works with PHPUnit and Codeception.
Installation
composer require "codeception/assert-throws" --dev
Include AssertThrows
trait it to a TestCase:
<?php class MyTest extends PHPUnit\Framework\TestCase { use Codeception\AssertThrows; //... }
Usage
Catch exception thrown inside a code block.
<?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); });
You can optionally test the exception message:
<?php $this->assertThrowsWithMessage( NotFoundException::class, 'my error message', function() { throw new NotFoundException('my error message'); } );
License
Codeception AssertThrows
is open-sourced software licensed under the MIT License.
© Codeception PHP Testing Framework