Testing if your code works is an important part of programming. We can manually test that our application works but this is not always as accurate and in depth as we want. You will also not catch all the bugs. You can not manually keep testing every possible case that can go wrong. It is impossible to manually test for every possible issue. We as developers should do unit tests so that we maximize our effectiveness in discovering bugs and minimize the amount of bugs that get to production.
What is Unit Testing?
Unit testing is a testing method by which small units of your code is tested. A unit is the smallest possible part of your code that can be tested. The reason why this unit should be small is so its easier to design, execute and validate if it actually does what it is expected to do. We assert that this unit is doing what it should do.
Why do I need tests?
You might ask to yourself, why do I need tests for code that already works? Well what happens if new features gets added? New bugs get introduced in code that you did not change. Wouldn’t it be nice if there where tests that would catch this? And that you don’t have to worry that much about the code that has these tests? This also allows you to refactor code without the fear of breaking anything. When you have tests you can actually change that ugly piece of code that no one dares to touch.
But also imagine if all your important business logic code has small unit tests and then a new developer joins the team. Well you don’t require any documentation, he or she just needs to look at all the tests to figure out how the code works. This means your tests are your documentation. When you write tests, you are also writing documentation. Isn’t that nice? But why is not everyone doing this?
It consumes to much time
Writing tests costs time because the reality is that you need to write more code. But is this a valid reason? Unit tests will help you to catch bugs because you can easily run the tests you created again.
So when it is easier to do the same tests, then doesn’t that save you time in the long run? Doesn’t this mean there will be less bugs in your code? And in return this actually means less time spend fixing bugs that you did not catch? In the long run it actually means that you can develop a bug free application a lot faster. And whenever a bug pops up, you write a unit test and now you can keep validating your code to make sure that nasty bug never returns.
Unit testing will allow you to find bugs earlier. This means less bugs in production where fixing bugs are more expensive. Isn’t that a money and time safer in the long run?
Complex code is untestable
You should not write complex code that is impossible to test. Writing code according to OOP and clean code principles means you don’t create complex code. Your code should be simple (KISS) and easy to test. If you create complex code that is impossible to test, how can you expect your colleagues to understand how this code works? How can this code be clean and decoupled from the rest of your code?
In short this means that Unit tests might be gate keepers to keep overly complex and coupled code out.
Testing is boring
Writing unit tests might be boring if you write all of them after you wrote all your code. You are then validating code that is already working. If you apply unit testing then you should switch around by writing tests and developing code. Make creating tests fun. Don’t do them at the end but preferable to it will developing.
Where do I start?
Most of the time people fear starting new things because they don’t know where to start. When first starting with unit testing I suggest using it when fixing bugs or writing new code. Any changes or new code you add should have a unit test to validate it. Don’t start creating unit tests for all your code, that is to much work and will overwhelm you. Try to ease into it. Practice and learn.
How do I convince my boss?
Don’t convince your boss with fancy words and talks but with actions. What you should do is start implementing unit tests. You should estimate your code as usually, don’t mention that you need extra time to create unit tests. After you are doing unit tests after a while it will catch on that your code has less bugs and maybe your colleagues will catch on and try it for them self. Your boss don’t care about how it’s done. He cares about results.
If you are not writing tests now, then you should start doing it. If you are not writing tests to validate your code in 2018, then you should really start thinking about this. There is no reason not to do unit testing, and if there is, please let me know. If you want to be a professional developer then your goal for this year should be to write unit tests. The end goal for every developer should be to deliver code with no bugs. It might sound impossible, but unit tests will help you with this.