Assert exception was thrown without stopping a test

1.4.0 2024-02-06 21:28 UTC

This package is auto-updated.

Last update: 2024-05-06 21:58:07 UTC


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

Actions Status Latest Stable Version Total Downloads License


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

Include AssertThrows trait it to a TestCase:


class MyTest extends PHPUnit\Framework\TestCase
    use Codeception\AssertThrows;



Catch exception thrown inside a code block.


$this->assertThrows(NotFoundException::class, function() {

// alternatively
$this->assertThrows(new NotFoundException(), function() {

// you can also assert that an exception is not throw
$this->assertDoesNotThrow(NotFoundException::class, function() {

You can optionally test the exception message:


    NotFoundException::class, 'my error message', function() {
	throw new NotFoundException('my error message');


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

© Codeception PHP Testing Framework