Passing along the knowledge.
This past week I helped an intern at the office understand the beauty of design for testability and writing unit tests. It was a nice experience and good preparation for the next teaching sessions that’ll come my way.
Granted, I am no expert, but I am glad to pass along any kind of knowledge I have. It is rewarding to help others understand things which perhaps took one several hours of reading and months of practice to get. I wished I had had, when I was studying, someone to explain these things to me, as clear as I explained them to him. When I was at school, they talked about the different design patterns but we didn’t really get a grasp of how they could really be used in practice. It’s a whole other world when you are explained the different creation patterns (abstract factory, factory method, builder, etc.) together with the benefits of dependency injection and separation of concerns. Sure, we got the basic idea, that we could have a factory which would return a Hawaiian Pizza or a Pepperoni Pizza but didn’t get the full potential of these patterns as when you show them in terms of dependency injection when designing for testability. Now the patterns don’t only help you have a menu with many different kinds of pizza, but they help you have a higher decoupled design, which in turn frees you to inject fake objects and write better unit tests.
The problem with many sources of information about unit tests is that they just explain what they are, and the different xUnit frameworks that help you run your tests in an automated manner. But in reality, the most important thing about testing is the way your classes are designed. If your classes are not properly decoupled and the Demeter Law Enforcement has you on their most wanted list, you will have a hard time writing unit tests. Actually, most of your classes won’t even be unit testable because they can’t be isolated properly.
I went a little out of topic there… I guess what I wanted to say is that when teaching someone about unit tests, you shouldn’t be thinking of teaching someone about testing but about design for testability. In the agile world (using an agile process for software development) the traditional steps of analysis, design, implementation and testing are blended in all together. If you think about how your classes can be tested when you design and code them, it will save you a lot of time when testing. Actually, there’s a movement called Test Driven Development where you write your tests first and then start coding to make them pass, so your unit tests are your design, but that’s another topic.