News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Lars Tesmer's Blog:
PHPUnit Better Syntax for Expecting Exceptions
September 05, 2011 @ 10:15:25

Lars Tesmer has an alternative to testing xceptions in PHPUnit that's a bit more flexible than just a docblock comment definition.

My main issues with this way of expecting exceptions are: The expectation is pretty far away from the location you'd normally expect to find an assertion. Usually, an assertion can be found at the bottom of each test function, whereas with the current method PHPUnit uses, it's at the top of the test-function. Additionally, it's an annotation "buried" in a comment which is easy to miss. Finally, PHPUnit will watch for an exception thrown by any of the code inside the test-function.

To replace it, he's created an "assertThrowsException" test that takes in the exception type to test for and the code to test for the exception (via a closure). He has his proof-of-concept posted on github if you'd like to give it a try. This also allows you to test for more than one exception in the same test, possibly as a result of slightly different conditions.

0 comments voice your opinion now!
phpunit unittest exception custom github


blog comments powered by Disqus

Similar Posts

Mike Lively's Blog: PHPUnit Database Extension (DBUnit Port)

Zend Developer Zone: Improving Code Quality with Flash Builder 4 and PHPUnit

Sebastian Bergmann's Blog: Isolated (and Parallel) Test Execution

Matthias Noback: A better PHP testing experience Part II: Pick your test doubles wisely

Sandro Groganz's Blog: Syntax Highlighting & Migrating Permalinks in WordPress


Community Events





Don't see your event here?
Let us know!


library laravel language interview symfony voicesoftheelephpant update opinion tool security release application version package community composer podcast introduction framework series

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework