Phpunit: how to continue test event if the assert fails

Created on 6 Jul 2014  路  2Comments  路  Source: sebastianbergmann/phpunit

is it possible to use kind of 'soft assertion' that's allow continue running the test event if the assert fail?

Most helpful comment

The reason for the current behavior is that assertions throw exceptions when they fail. If you want to "continue running," you can use a try/catch block:

try {
    $this->assertTrue(false);
}

catch (PHPUnit_Framework_AssertionFailedError $e) {
}

// continue running

Just make sure you re-throw the exception or call $this->fail() by the end of the test if an exception is raised.

All 2 comments

The reason for the current behavior is that assertions throw exceptions when they fail. If you want to "continue running," you can use a try/catch block:

try {
    $this->assertTrue(false);
}

catch (PHPUnit_Framework_AssertionFailedError $e) {
}

// continue running

Just make sure you re-throw the exception or call $this->fail() by the end of the test if an exception is raised.

this also works:

   $this->expectException(Exception::class);
   try {
        $this->whateverThatGeneratesTheException...
    } finally {
        //run any code afterwards
    }
Was this page helpful?
0 / 5 - 0 ratings