Skip to content

Repository files navigation

AssertThrows

Handle exceptions inside a test without a stop! Works with PHPUnit and Codeception.

Actions StatusLatest Stable VersionTotal DownloadsLicense

Installation

composer require "codeception/assert-throws" --dev

Include AssertThrows trait it to a TestCase:

<?phpclass MyTest extendsPHPUnit\Framework\TestCase
{
useCodeception\AssertThrows;
//...
} 

Usage

Catch exception thrown inside a code block.

<?php$this->assertThrows(NotFoundException::class, function() {
$this->userController->show(99);
});
// alternatively$this->assertThrows(newNotFoundException(), 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() {
thrownewNotFoundException('my error message');
}
);

License

Codeception AssertThrows is open-sourced software licensed under the MIT License.

© Codeception PHP Testing Framework

About

Assert exception handling without stopping a test. For PHPUnit 6+

Resources

Stars

19 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages