When I was starting my programming journey I didn’t care about tests. I had no time for this, I didn’t understand why I should do this. When someone said that I should spend the same amount of time on writing classes and tests, my mind blows. I launched my first startup without tests – yes it was a maintenance nightmare.
Reasons for doing code tests
A few months later I started to learn how to write tests. Now, after many years, I don’t understand how code without tests can even exist. If you need a clue why you should write tests then check reasons presented below.
1. It’s easy to refactor code, modify and add new code
It’s the most obvious reason. Instead of clicking on the whole app or praying that you didn’t break anything with your changes you can simply run tests. Of course, the fact that you have tests does not mean that they are good enough to catch errors. So care about unit tests at least, create acceptance tests if you have time and hands to work on it. However, having full, well written, tests suite reduces the possibility of errors on both backend and frontend side.
2. Tests are kind of documentation
If you ever run rspec with
--documentation flag then you probably know about it. There is always not enough time to write documentation but with well-written tests and proper wording, you can automatically create documentation while creating tests.
3. Others can understand
New team members or junior developers would appreciate this. You can have comments on your code but proper tests are the best explanation for your classes and modules.
4. Verify if application meets client and users expectations
In a perfect world client writes testing scenarios but in a real world, you have to do this by yourself. The easiest way is to implement three steps cycle: red, green and refactor. First set expectations and then take care of writing code that would handle them.
I hope now you have fewer doubts about writing tests after reading my reasons for doing code tests. Let me know about other reasons if I missed any.
Having problems with tests for your Ruby on Rails app?
Image source: pexels.com