by simonw · 1y ago · 0 comments
In my opinion tests are the ultimate productivity hack for building software. In exchange for a relatively small up-front cost (if you know what you are doing) they let you move so much faster! You can make changes with confidence that each change won't break your existing functionality, without having to spend hours on manual testing for every change you want to ship.
This goes for smaller personal projects too. I gave a talk about this a few years ago, originally titled "Massively increase your productivity on personal projects with comprehensive documentation and automated tests" but then renamed (after I'd pitched the talk but before I presented it) to "Coping strategies for the serial project hoarder". Video, slides and notes here: https://simonwillison.net/2022/Nov/26/productivity/
My issue with tests in my side projects is that it kills my flow. I need to actively iterate on the code that matters and feel out the results. At what point should I be writing tests?
I can only think of a point where I am certain this is where I am going to stay at and then I write some sanity regression tests.
"At what point should I be writing tests?"
Once the code does the right thing, and you want it to keep doing the right thing. Don't try to write the tests first, that's a productivity anti-pattern.
Uh, this isnt great advice.
If you're spiking then sure, dont write a test at all.
If you're writing production code you should have a clear picture of what it should do before you start. Your test should validate that so it makes sense to write that next. It serves as a natural transition from your requirement to youe code.
Ive written a lot of code without tests at all for good reasons and a lot of tests before code but ive never witnessed a good reason to write a test after writing the code. At best it is almost as beneficial.
It's unconventional advice and goes against industry "best practice" - plenty of people will imply that if you're not writing your tests before your implementation you're "doing it wrong".
I think that advice causes people to skip writing tests entirely and feel guilty about it, because often writing test first really does slow down your development process substantially in many cases.
My experience is that writing tests after the implementation gives me all of the value of writing the test before but takes genuinely about half the amount of time.
You can't do snapshot testing if you force yourself to write the tests first!