A Path Less Taken

Breaking with convention in a very conventional fashion. Powered by WordPress

"What would you attempt to do if you knew you could not fail?"
Dr. Robert Schuller

Wednesday, July 21, 2010

Category: PHP Development Tags: , Author: JJ 1 Comments

I was recently working on a generic container class in PHP that implemented allowed, required and group behaviors related to assigned properties. While coding the class I built a test class using PHPUnit that had 7 tests and 33 assertions. After using the class on a limited basis I determined that the internal implementation left something to be desired. Unswayed I dug in and re-implemented the bulk of the internal methods without changing much of the interface of the class. I ran the unit test again and found that only 8 of the assertions related to the modified behavior needed to be updated. After I changed those assertions the class passed all the tests.

I know that many you are asking “what’s the big deal about that?” The big deal to me is that I’m often reluctant to write tests because I’m a hobbyist developer and I’m always anxious to get straight to the code. I guess you could say that this simple experience has changed my point of view. It is very powerful to be able to change a class as fundamentally as I did and still be able to validate that it behaves as desired with such ease. The benefit of all the upfront work in building unit tests was clear to see in this case.

If you have a similar experience or just a point of view one way or another on unit testing please post a comment below and share it with me.

One Comment to “Unit Testing FTW”

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">