Write high quality unit tested Actionscript 3 for fun and profit…

For the past year or so unit testing has been one of my favorite subjects. There are tons of books on the subject, but several really stand out. Here are my favorites so far:

Clean Code by Uncle Bob Martin

This book does cover unit testing a bit. It isn’t the focus of the book, but regardless it sits here at the top of my unit testing list. Why? Because the hardest part of writing unit tests is writing code that can be tested in the first place. Get it. Read it. Learn it. Seriously. Uncle Bob rides in the vanguard in the battle against horrible dirty code.

The Art of Unit Testing by Roy Osherove

This is a small book, but it is stuffed to the gills with distilled information on the subject of unit testing. Don’t be put off with the .NET examples. It doesn’t make a difference at all. There are a couple of chapters that are .NET specific that you can just skip, but 90% of the book is killer information on testing your code.

Working Effectively With Legacy Code by Michael Feathers

Like Clean Code, this book isn’t specifically focused on writing unit tests, but it totally is. Feathers defines “legacy code” as “code with no unit tests”. You can quibble about the semantics of this definition, but you’d be missing the point of this kickass book. How do you take a pile of code with no tests and make it better in a safe incremental way? How do you stop producing legacy code at a furious pace? That’s right, the code you wrote today is probably sitting in the “legacy” column already. Even if you are writing tight unit tested code you will likely stumble into code that isn’t, and this book has tons of good advice on how to deal with it.

xUnit Test Patterns by Gerard Meszaros

This one is a monster. It is a duplex book with a strong narrative up front and a massive patterns catalog bringing up the rear. if you read a lot of unit testing books you will “feel” this one under the best ones as the giant upon whom they are standing. This is source material here, and might be my personal favorite. I think The Art of Unit Testing makes for an easier (quicker anyway) read and if you were to choose one or the other go for it. Don’t choose one or the other though. Read both!

Growing Object Oriented Software Guided by Tests by Steve Freeman

While the above books touch on the concept of TDD it isn’t their focus. This relatively small volume is a great introduction to the subject of test-driving your code. It steps out of simple unit tests and gets into the realm of integration and acceptance testing. It has a fabulous explanation of mocking and is a fantastic introduction to the “testing layer cake” that high quality maintainable applications are guarded with.

So what is the take away from all of these books? Unit testing is actually pretty easy. Writing clean testable code is harder. We (developers) tend to complicate unit tests a lot. We write them as integration, acceptance, or other more complex test types that are orders of magnitude harder to write and maintain. A unit should be tiny and easy to test. If it isn’t, then something is probably wrong with the unit or the scope of the test. Unit tests are for developers by developers and shouldn’t be considered an afterthought or some line item on a managers spreadsheet. A high percentage of test coverage should not be the goal. Quality over quantity. Confusing unmaintainable tests are a liability to a project. Concise readable unit tests can be a trusted asset.

As with all code, complexity is easy. It is the default. Elegant simplicity is much more difficult. At the end of the day, cramming all the information the above books offer into my head has me writing dead simple unit tests. It seems weird as I examine it, but that’s how it has worked out. Study up and write some high quality tests. You will be well thanked by both your fellow developers and your future self.

  • http://twitter.com/tonylukasavage Tony Lukasavage

    I've been looking for a reason to cram some TDD education into my schedule. I think this post is enough to make it happen. Great work!