I just want to preface this post with saying that this whole post is just my experience and will give you no practical advice or opinions.
Four years ago when I was 20 years old I was given my first software development job. Not only was this my first big job but it was at a fortune 500 company where I was surrounded by experts so I never felt like I really knew anything in comparison. Luckily for me these experts were amazing mentors and I owe most of what I know to these people, BUT this didn't stop me from always feeling like I didn't know enough. I remember the first time one of the senior devs paired with me and showed me his tests It felt like this already massive amount of knowledge I was lacking tripled in size. I had a lot of questions
- Why write tests? They seem like they take a long time.
- How do you know what to test?
- What in god's name is a mock or a stub?
- Why would I need to mock something, doesn't that defeat the purpose of a test?
- What is the difference between unit, behavior, and integration tests?
- What is black/white box testing?
- Now I'm testing a react component, is it inappropriate to simulate a click event in a unit test?
- I'm scared I will spend all this time writing tests that no one will run except me, should I still write them?
This list could go on forever really. The idea of writing tests seemed so foreign, I mean I was still learning how to write code, I didn't have time to learn this nuance that is testing.
Having spent many hours of many days trying to learn how to write tests I concluded that testing was not for me and that it slowed down the software development process too much to be valuable. My team didn't have any hard requirements like the code must have 70% test coverage or anything silly like that so it was fairly easy to avoid. Although I was happy avoiding testing I couldn't help but feel that every time QA would find a bug that maybe I could have avoided that bug if I had written tests. It was worse when something I knew to be working before would get a bug filed a couple months down the road saying it no longer worked. It felt like I would write a feature and break 2 others, and I wouldn't know it until QA or a user would tell me about it. Having broken many things I really started to fear making changes. Not feeling confident in my code was and is the worst feeling in software I have had.
I had come a long way in my journey to learn how to test. Let me try and rephrase that... I had come a long way in my journey to write tests that were fragile. I was no longer scared to write tests, especially unit tests. I knew how mocks worked and I would mock anything in the world if it meant I could isolate my test in it's own little tiny sandbox. My solution was to unit test everything in order to conquer my fear of making changes. This led to me wasting a lot of time refactoring tests over and over because an implementation would change and all of a sudden I had to change 6 test files because they relied on that implementation. My productivity was at an all time low, even though I was writing more tests and having less bugs, but there had to be a better way.
I had given up on the idea that everything needed to be unit tested. In fact my opinions were quite the opposite, and instead I looked for the right test for the job. There was no need to unit test a controller that relied on 8 other classes to be injected in the constructor, this would only lead to a fragile test, instead this would be better suited for an integration test. I was beginning to instinctively understand what kind of tests would be most appropriate.
I teach other developers how to test, and I feel extremely confident in the tests that I write. My tests rarely break for the wrong reasons, and I spend more time developing than worrying about how I'm going to test this feature. My biggest problem is implementing build systems that make sure tests pass before code is merged.
I wrote this article because I want junior developers to know that your not alone if you find testing difficult for a long time. Software development is hard and anyone that says otherwise is either a genius or naive (most likely the latter.) It doesn't help that everyone has their own opinions when it comes to testing and it seems that every article you read contradicts the last article. Just know that your not alone if you feel this way, and you will one day conquer those tests and feel proud!
Maybe one day I'll write about my own opinions when it comes to testing.