Writing Automated Tests is a Learned Skill
The chart below represents the first 800 builds of our process automation tool set (around six months work). The green dots represent the number of test cases passing for each build and the red vertical lines indicate that a build has failed one or more test case.
We try and do test driven development but it’s usually just on a local branch so the way I choose to think about this chart (which is from our build server) is that every red line is an instance in which automated testing has made my life easier. There are over 200 red lines. That's +200 in our Delivery Balance Sheet. Our current unit test coverage is 91%.
In the last six months I have learnt a lot (by experience - always the best way) about writing unit tests and next time I’m interviewing someone (for a vaguely relevant role) I will definitely ask a question about how how to write good unit tests. I hope that the answer will demonstrate experience rather than just knowledge.
Anyway, to the useful bit. There’s a bunch of stuff on the Internet about Mocking in Python (lots of it good too) but, as a complete beginner I had to fumble about a little to i. get it working and ii. do it properly. The code below demonstrates a simple class and an associated unit test.
I'd like to quantify the exact point at which point during the last six months the time and effort spent on unit tests became justified by the time saved on defect fixing but any attempt would be a little contrived.
Instead I’ll pitch it this way:
I (and the members of my team) are significantly happier working in an environment with comprehensive unit tests in place. I feel confident to develop and refactor code as necessary to ensure that my current design is the best it can be for my current understanding of the requirements.
I have confidence (and take pride) in the new code that we develop and writing unit tests has truly become just a part of development. I know that I am more than a little late to the party but I’m pretty sure that I’m not the only one. If I have learnt three things over the last six months they are that:
Quality centric features are essential in technology selection.
Automated unit tests make developers happy.
Happy developers make everyone else happy.
Go and write some automated unit tests. Keep trying to get better at it. It will make you happy.