News Feed
Sections




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

Padraic Brady's Blog:
Unit Testing Multiple Assertions And Lazy/Shallow Testing Are Evil
February 13, 2009 @ 09:31:33

In a new post today Padraic Brady takes a look at unit testing and two of the bad practices that can develop over time when writing your tests - overloading tests with assertions and lazy/shallow tests.

In short, every test you write requires that you setup the test environment, create a scenario for possible failure, add an assertion, and then ensure the source code makes that assertion pass. This requires code - sometimes a lot of code. So adding multiple assertions to each test minimizes the work needed to write tests, since using multiple assertions takes advantage of existing code to avoid writing new stuff to clutter your test classes. It can also help to tackle multiple but related results in the same test.

He gives brief examples of both instances and some of the problems associated with them. Multiple assertions, while nice for fine tuning the results of the testing, can also confuse since, if one assertion fails, the whole test fails too. The other end of the spectrum is a problem too - writing tests that don't do enough to really check the information passed in. These shallow tests can lead to issues down the line if unanticipated data might come along.

Really, its all about finding that "sweet spot" in developing tests - not too much, not too little - and keeping it simple while not missing out on assertions that need to be made.

0 comments voice your opinion now!
unittest phpunit lazy shallow multiple assertion example


blog comments powered by Disqus

Similar Posts

Label Media Blog: Test Driven Development with PHPUnit

Kae Verens' Blog: Hosting multiple sites from the same CMS engine

Qafoo.com: Practical PHPUnit: Testing XML generation

Mayflower Blog: Traits in PHP 5.4

Anthony Ferrara: Programming With Anthony - Dependency Injection


Community Events





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


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

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