I wanted to write about how I'm deciding to form a better opinion on testing, then I got side tracked into thinking the various stages of opinion having I've gone through. Decided to leave it in. Here is what I came up with:
Stage 1: Willy-Nilly opinions
I would call it my naive self having an opinion on everything, most of them dictated by the status quo of the people I was surrounded by at the time. You've got to start somewhere, so as long as you grow out of this stage, you are fine. I think I was able to, as I moved to another country and my opinions no longer made sense. Which leads to,
Stage 2: All my opinions were false, so I'm having none
This is the dramatic phase of opinion puberty, if you are lucky, as I think I have been, a lot of my opinions fell apart in good conversations really quickly as they were not based on experience, the foundations were weak. So I got all dramatic and gave up on all my opinions. Experience and diving deep into interests will help you grow out of this stage.
Stage 3: Some of my opinions were valid
These opinions might match the ones you had in stage 1 and a lot of things might be conflicting. The key difference is that these are molded by experience, they are not fully concrete yet, but the base is quite solid. As new experiences happen to you, you'll be able to refine these opinions, if they are warranted. Overall, these are quite solid, and it will take someone wiser or your own meandering experience to make dramatic changes or drop them altogether.
Stage 4: ???
Haven't reached this stage, yet as I'm still quite new to stage 3. Maybe one day my gray bearded wise self write about it, if he can be bothered. I doubt he will be.
Okay, that was long winded intro to why I have recently decided to write more tests, even on smaller projects. Mainly because I want to understand what to test, when to test, and when it's not needed.
Testing more
I have been skeptical of testing for a while, recently I came to the realization that it had more to do with how I was testing than testing itself.
Reading through the pragmatic programmer section on testing, I came across the section on property based testing, which I found quite practical. I'm focusing on a smaller subset of property-based tests that focus on verifying the expected state before and after a function executes. For instance, when testing an order function, I assert that the sum of the ordered amount and the current stock after the function runs should equal the total stock before the order was placed. This ensures, that some of my poor assumptions during code are caught.
It took a lot of effort to get started as I was really reluctant of testing. Mostly because, haven't done much testing in a while. It did not take a lot of time to see the immediate benefits. The first one was that, I was writing code very differently to make it easier to test. Smaller methods and using interfaces instead of concrete types for services in http
handlers. Nothing new here, but I've realized why it makes more sense. The code is less coupled, I can swap out dependants as long as the interface is satisfied. To name a few.
The other benefit I've noticed is that, I find improvements, redundancies in code as I'm going through the code that is under test. Which I would only do if there were bugs that were caught.
Just to be clear, I haven't gone all TDD, as most of the time I don't know exactly what the outcome should concretely be, upfront. Also, I have a tendency to prematurely ej optimize, so I only test after the feature is working completely.
Lastly, if a peiece of code feels flaky or have nuances that I can barely remember the day after. I leave a little //TODO: test this
, to come back to it later. When I sit down to write tests, I to get a few of these Todo's done. That being said, I have a lot of Todo's in my code atm.
Finally, I'd like to say that, all though these are some of the things I'm doing, reality is far more nuanced (surprise surprise). Some of my tests are not perfect, a lot of the Todos still remain to do. But, there are far more tests, my code is a lot better than it was say two months ago. The more tests I write the easier it becomes to write the next one. Some mistakes were made, some brittle tests were written and continue to be written, I'm just not aware of them yet. The focus is always, progress not perfection.