About two years ago I joined a team working on a well-established application with a long history. After a quick ramp up period, I began fixing bugs on my own. Nearly every issue I fixed broke the corresponding unit tests. Initially, I resented the unit tests, they represented one more thing to deal with in an already complex application with its own learning curve challenges.
I guess you could say that I've had a gradual change of heart since those early days.
I recently began working on yet another project for the same client. This project is for a well-established production application that can be described as a super set of the one I started with. We're currently involved in localizing the app and the changes are breaking most of the unit tests. These days, however, rather than holding a grudge, I'm discovering that the process of fixing those tests is an aid to ramping up rather than a barrier. Fixing a broken test forces you to understand the minute details of the code you're testing.
The test driven development approach says that you write the test first (to establish what the method should do) and then write and rewrite the method until the test passes. While that process doesn't apply in my situation, the act of writing new unit tests against already written code and modifying existing tests to account for changes has been invaluable. For me, it provides a structured way to approach the process of intimately learning the inner workings of the application I'm helping to build.
Also, writing tests for code written by someone else allows me to approach that code with a fresh perspective, uncluttered by any preconceived notions about what the code should do. This initiates a process of inquiry that, at the end of the day, leads to a deeper appreciation for the business rules and how they are implemented. My most recent tests have revealed that the current implementation is not always bug free. And it's always better to catch and fix bugs before they get out into the wild.
If you're a unit-test-hating newbie, my advice is to stick with it. Pick a testing framework, learn it well and use it thoroughly. The benefits may not be immediately apparent to you now. But your perseverance will eventually result in better quality code. Guaranteed.
If you're a seasoned veteran, chances are that this is nothing new to you. But regardless of which side of the hill you're on, I'd love to hear your opinions. Are you invested in unit testing already? What were your challenges when you first started? What challenges are you facing now? Feel free to share your thoughts in the comments.
Happy coding and happy testing!