Unit Testing - a realisation that it's absolutely worth it
Thursday 19 November, 2020
Tags: development musing
While I've been working on a personal Flask project, building an API for use in an example game, I wanted to make a structural change that has finally made me come to realise why I started this project using Test Driven Development from the ground up as a testbed for future endeavours.
I'd always wanted to fully jump on the testing train, mostly because I enjoy that side of coding and finding all of those edge cases that a lot of us miss. A new project seemed like the prime opportunity and I'm starting to come around to the idea of why it's so beneficial to work with a test first approach.
I realised that my main routing file was getting rather large as more API endpoints were being added, causing it to become unreadable at times having to scroll through the few hundred lines each time I viewed it. I had a card in my inbox (that I'd deferred once or twice!) to try and convert my file structure to use Flask Blueprints as it increased in size and it seemed like the in-built solution for route separation. I wanted to finally tackle the restructuring required.
Instead of two days, it took two hours. The fact that I had a previously passing test suite that I could use as a reference to work in a 'divide and conquer' style worked absolute wonders for me. I was focussed on getting tests to pass and deciphering the error messages neatly provided in the pytest outputs rather than wondering why my code is failing silently. The feeling of getting groups of tests to pass and mentally checking them off rather than waiting for the next bug to come up after you fix one in the 'chain' was exponentially better for my motivation. Yes, it's a small application in the grand scheme of things but the test suite gave me a clear starting point and that clarity didn't go away until the final test passed.
A completely new way of tackling development for myself, where writing the tests is taking more time than writing the functional code. A new norm but one that I'm not against. I'm happy to have begun the journey and will strongly consider a test-first approach for all open and closed-source projects going forward. An important realisation for myself personally and I hope you somewhat enjoyed my experience.