Practical value of unit tests

Hello everybody,

Here will be another interesting benefit of writing unit tests which I've discovered recently for myself. 

Simplified testability

Imagine, that for you as for developer arrived some kind of bug. After investigation you've found that bug is caused with imporper method functionality. How can you fix method without unit tests? Way will be like this:

  1. Add code fix
  2. Execute application and make effort to re-prodcue bug
  3. If bug is not re-produced, you make commit/push and enjoy life
  4. If bug is not fixed, go to steps 1 - 3 one more time.

Here is how those steps can look like in unit test:

  1. Add unit test that feed into method with bug wrong values
  2. Add fix to code
  3. Execute test
  4. If test didn't cause bug, then execute application and test correctness of code in application
  5. If bug is not re-produced, you make commit/push and enjoy life
  6. If bug is not fixed, go to steps 1 - 3 one more time.

From the first viewpoint it looks like we have more steps involved and a bit more tricky additions. Yes, that's true. More steps. But more steps not nesessary means harder life. 

Consider following use case. 

Typical application, especially some enterprise level requires from end user to repeat some set of actions. For example login, navigate to some form or windows appliation, enter 3 - 5 items in inputs ( sometime even 10 items ), and press at some button. One more point to keep in mind is that startup of enterprise application is not fast process. Sometime application can load for a quite long time. 

And now compare this approach with unit tests. Developer can hard code some values that reproduce some data which cause error and then work only with target method just via execution of Single test or set of tests. In such case developer omits start up of application, repetative data entrance, mistakes in typing data, etc. 

Faster execution and fixing of bugs

For the same reason developer can faster execute needed for his attention part of code. For computer it's easiere to execute some fragment of code then to execute whole bunch of libraries. Also because of hard coded values that can cause bugs, developer can just click on Test, and press there Debug.

Better focus on detail

Imagine, that you as developer see some method time and again. Time and again while running your unit test. What are the chances, that your brain will find the way to optimize that method? Chances are pretty high. But what if your brain is exhausted with switching between developer mode and user of your app mode? You'll got distracted, upset, even angry, especially if you need to enter the same data again and again. But if you coded your test well, then computer by himself will enter data instead of you as developer again and again and you'll have a chance to see by yourself how to make buggy part of code not just no-buggy, but improve it's performance.


So, if you don't use unit tests in your company think again about practice of using them. Their usage initally can take some time and practice but in the end it will give you win in plenty of areas. 

No Comments

Add a Comment