Though there’s been some interesting discussion on whether unit tests are waste, I’m going to thank today me for saving future me’s ass by using unit tests.
There’s a very interesting talk by Ian Cooper on how unit test implementation can catch you out:
This is why I’ve published the test smells. Many of the techniques in there are a sign that your testing may be leading you astray, or may be hard to maintain – the very opposite of why we make tests.
Today, I did a half-and-half job of writing code and tests. I could have approached it differently, but a lack of familiarity with the problem meant that I did things from a few angles:
- I wrote an algorithm test first – it was just a regex thing
- I made an API to use the algorithm and tested that a bit
- I wanted the API to delegate to another API when my algorithm was done – so I played with that other API in postman, and knocked together a client from my learnings
- I experimented with whether my API client would work in real life
- I sped things up by using the API client in a unit test to see if I could debug it
- When I was happy, I captured some responses from the real API so I could use them as mock responses for my client
It’s a test-driven approach, but my lack of familiarity with the API and the mocking of API calls in TypeScript meant I went a bit round and round it to get it there.
Then I decided to probe some edge cases around non-200 response codes, especially a particular code to do with rate limiting on the API.
It turned out that my initial rough implementation had a typo in it, which meant that it looked right and appeared to work, but would not cover this rate limiting scenario.
It was a bit of logging that would never have happened, leading to some sort of future support mystery. I could have ignored that edge case, because I’d seen the whole thing working with my own eyes. I should have taken more of a test-first approach to it, to be fair, but the point is that I didn’t let the matter slip.
I’d decided to leave myself with the best possible coverage as a bug detection aid.
Future me will be glad that this issue isn’t going to be an issue.