All Projects → Codeception → AssertThrows

Codeception / AssertThrows

Licence: MIT license
Assert exception handling without stopping a test. For PHPUnit 6+

Programming Languages

PHP
23972 projects - #3 most used programming language

AssertThrows

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

Actions Status Latest Stable Version Total Downloads License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].