News Feed
Sections




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

Chris Hartjes:
Testing Smells - Try/catch
May 01, 2013 @ 11:42:29

In this new post to his site Chris Hartjes gives an example of what he calls a "testing smell". This particular illustration deals with the poor handling of testing and exceptions with try/catch blocks.

As part of a project to migrate the PHP code at work from PHP 5.2 to PHP 5.4, I'm using our extensive test suite to look for instances where something that changed between the versions of PHP that we are using has caused some unexpected behaviour. In one of our code bases, I found some tests that are exhibiting a test smell through their use of a try / catch block in the test itself.

He includes a (contrived) example showing the use of an exception in a unit test to run an assertion in the "catch" for the test to pass. He points out that this particular check is being done to see if the user input is valid...and that it's a bad way to enforce it using exceptions. He also suggests that if you have an "if" situation, don't use one test with logic in it, write two tests. He mentions a disenting opinion but notes that a failing test is a failing test, regardless of what caused the failure.

0 comments voice your opinion now!
unittest smells try catch exception handling if

Link: http://www.littlehart.net/atthekeyboard/2013/04/30/testing-smells-try-catch

blog comments powered by Disqus

Similar Posts

Dallas PHP User\'s Group: PHP5 Exception Handling (presentation)

Maarten Balliauw: Running unit tests when deploying to Windows Azure Web Sites

Mike Lively's Blog: Introducing Phake Mocking Framework

Ralph Schindler's Blog: Exception Best Practices in PHP 5.3

ZendCasts.com: Unit Testing Doctrine 2 Entities


Community Events





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


podcast composer interview community version release library language laravel voicesoftheelephpant list tool security artisanfiles symfony series opinion conference introduction framework

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