Very well written. I loved how you gave bad examples and proceeded to correct them.
And you raise some good points too. I found many testing patterns I learned from experience that I wish someone had told me about when I was just getting started with TDD. Especially the one about not giving too much thought to the DRY principle during tests.
One of the hardest parts I found about TDD was knowing exactly what to test.
You start off thinking you must test every little bit of code, but this just creates major overhead and often quite brittle tests. I've come to the conclusion that it's better to think about it as input/output.
Only testing for what's coming in and what's going out. The internals of how we get that expected output aren't necessary to test. This has helped greatly when writing tests and hopefully is correct.
Other than that, TDD has really helped to encourage keeping functions simple and modular. When something becomes (or looks like it's becoming) difficult to test, it often means the function is becoming too verbose and should be split up into smaller more manageable/testable pieces.
Very well stated!
Thanks! That's reaffirming :)
I was in the same boat and recently have been mentoring an organization on testing ... prompting this article. Thanks!
Good stuff Bob!
Great set of examples! Love this read
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.