Skip to content

Latest commit

 

History

History
13 lines (10 loc) · 672 Bytes

assert-exception-not-thrown.md

File metadata and controls

13 lines (10 loc) · 672 Bytes

Assert that an exception is not thrown in a PHPUnit test

While asserting an exception to be thrown in a PHPUnit test is straightforward, the opposite is a bit more difficult: asserting that a specific exception is not thrown.

By wrapping the code potentially throwing the exception in a try/catch statement, it becomes possible to assert that the variable meant for holding the exception remains uninitialized. If the exception gets thrown, it will be assigned to the variable, causing the assertion to fail.

Example

try {
    // Test code potentially throwing a RuntimeException goes here
} catch (\RuntimeException $e) {}
$this->assertNull($e);