Thoughts on Testing
Today, I spent some time reading about unit testing, as is my wont. Via Hacker News, I came across the Google Testing Blog, which has some really interesting ideas. Here are a few:
Reduce nesting and indentation levels by using guard clauses to return early or raise exceptions from functions when certain criteria are not met. This helps eliminate blocks of code that look like
if condition1:
if condition2:
if condition3:
doThing()
else:
raise RuntimeError("Condition3 failed!")
else:
logger.warn("Condition 2 failed; could not do thing.")
The second if
in this chain fails silently. In fact, this example is very similar to a bug I identified and fixed last week.
The advice to not overuse mocks is thought-provoking.
- I wonder how this advice can apply to the code base I'm currently working on. I've noticed that my tests are a little cumbersome to write, but mocks seem seem like a really good way to test situations where messages are sent and received over the network.
- One particular class I'm working on is stateful, and so requires triggers to transition from one state to another. Those transitions need to be verified.
- For these situations, I mock the
receive
method to return some pre-canned data, which triggers the transition under test. - To me, this seems like a good use of mocks, even if it makes the test code more verbose.
- For these situations, I mock the
- I suppose my description is more a use of mocks rather than an overuse of them. But this is something to bear in mind as the test suite grows in complexity.
I especially liked this piece of advice:
Leave the campground cleaner than you found it
- A codebase where a file is marginally improved every time it is modified will eventually get better.
- When there's a fear of changing or improving existing code, stemming from fears that doing so may introduce a regression, that is an indication of a deeper flaw. Likely, there are not enough unit tests or integration tests to provide reassurance that these changes are, in fact, safe.
- In which case, adding in those missing tests would be a huge improvement!