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

Vance Lucas: Handling Exceptions in Gearman Tasks (Even Background Ones)

Philip Norton's Blog: PHPUnit Skeleton Classes

Brian Swan's Blog: Running PHPUnit in Windows Azure

Sameer Borate's Blog: Raw vs. cooked PHP $_POST variables

Chris Hartjes' Blog: Metatesting: Understanding Mock Objects


Community Events





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


deployment voicesoftheelephpant introduction package release opinion series install podcast interview laravel community tips list bugfix api framework symfony language library

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