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

Patrick Reilly's Blog: Zend Framework Project Teams Need You

Matthew Batchelder\'s Blog: Writing Your Server-Side Ajax Handler

Kore Nordmann's Blog: Evil bugs in your code

Debuggable Blog: How To Execute Only Specific Test Methods in CakePHP Unit Tests

Matthias Noback: Test Symfony2 commands using the Process component and asynchronous assertions


Community Events

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


api introduction list symfony part2 laravel project framework php7 opinion programming interview application yii2 composer community language series example podcast

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