facebook
MENU

Blog

Xento is a product based organisation with more than 800 IT Professionals including 350+ PHP Professionals working with a healthy dose of creativity and innovation. We promote an open atmosphere, where team members freely voice new ideas and are involved in all phases of our growth. At Xento, working is not just work; it's fun. Nurturing individual talent, fostering team work and recognition are the key elements of the Xento Culture.

Unit Testing and its Importance

Category: Continuous Improvement, Software Quality

Unit Testing - a software development process

Application is developed, assigned to QA. QA creates a sheet full of bugs to be fixed and shares with the developer. If you are a developer, you would ask QA various questions – What the heck is this? How in the world could this happen to me? Why did this happen? We have tested our “application”, with every possible scenario on “browser”. Bugs are popping in, and we are fixing them. It takes quality time to fix those bugs. Probably more time to fix bugs than developing the piece of code. Quite a few times, client reports silly bugs which is embarrassing.

It might be the same bug you fixed weeks back, or yesterday itself. Debugging is sometimes not taken seriously. When in hurry, instead of checking whole application, QA as well checks the known areas with a few random clicks on the site pages. This is frustrating for clients/end users. Have you worked into this sort of the problem which is faced by many? There is a solution for this – UNIT TESTING, to get out of miserable life, to make lives better.

 

What is Unit Testing?

Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. Unit testing is often automated but it can also be done manually. A unit is the smallest testable part of the software. It usually has one or a few inputs and usually a single output. In object-oriented programming, the smallest unit is a method, which may belong to a base/ super class, abstract class or derived / child class.

Common Myths

1) Invest a lot of time
If we write unit tests, we are investing a lot of time in it. Time expended in coding unit tests keeps other features un-built and a number of backlog tasks increases. This is thought of as lowering development productivity. Unit Testing is a crucial part while implementing banking, insurance, payment-related applications, wherein a fraction of error costs the company.

 

2) My code works fine
I don't always test my code, but when I do - I do it in Production

Common excuse is, the application works, so there’s no real need to test it. If a newbie is assigned a task, does the changes, whilst the application breaks, developers are set on fire-fighting mode. That is where unit tests are considered a boon. If there were unit tests, the tests would have failed and issue would be brought to notice prior to being released.

 

3) Its boredom
Right?? It no fun to write unit tests. It’s all the same. Developers are geeks, trying to solve problems around the world as if the world would come to end among these unsolved problems!!! Unit testing is a valuable addition to the tons to work done, the long hours spent to build a functionality out of scratch. If the function/feature does not work as intended, all pain taken to finish it off and keep it rolling turns to despair. Everyone focuses on big tasks, fixing a bug is not a challenge, to track it down is seen as a waste of time. It’s not awarded. But look at the other side of the coin, a stitch in time saves nine. A little effort now saves a lot of time, later on, avoids frustration.

 

Developers are the one to perform and implement this first level of testing. This is referred as White Box testing. The developer is fully aware of the source code. Implementing all the code paths and including all libraries would be known to developers, so they know all ins and outs of the application. They create the base of unit testing setup, implementation, running tests, and making sure they succeed.

Benefits / Importance of Unit Testing

  1. Lets you validate your component design in isolation, making it easier to debug when there’s a problem. Bugs are detected early. This is cost effective for the project. Sooner the bugs are detected, sooner they would be fixed and chances are less that they would be transferred over to other places in application.
  2. Lets you test functionality which is either not exposed or not easily accessible from the program’s external interfaces, for example error handling and functionality which has no UX yet.
  3. Provides a cheap way to ensure you haven’t broken any major existing functionality across the system when you make cross-component changes.
  4. Provides a way for people unfamiliar with your code to verify they haven’t broken it when they are working with it. And, for them to see how it works.
  5. Automates what might otherwise be arduous manual testing. It helps keep you from getting lazy and not testing the impact of changes.
  6. The tests and the code work together to achieve better code. Your code could be bad / buggy. Your TEST could be bad / buggy. Often it’s the test that needs fixing but that’s still a good outcome.
  7. Unit Tests help you really understand the design of the code you are working on. Instead of writing code to do something, you are starting by outlining all the conditions you are subjecting the code to and what outputs you would expect from that.
  8. Unit Tests give you instant visual feedback, we all like the feeling of all those green lights when we’ve done. It’s very satisfying. It’s also much easier to pick up where you left off after an interruption because you can see where you got to – that next red light that needs fixing.
  9. Martin Fowler once said: “Imperfect tests, run frequently, are much better than perfect tests that are never written at all”. Implied that writing fewer tests is far better than having 100% code coverage achieved.
  10. Good unit tests, turn out to be a great document and define to other developers, what something is supposed to do.
  11. Code re-use is one of the advantages of unit tests. Migrate both your code and your tests to your new project. Tweak the code until the tests run again.

Unit testing is an investment, whose benefits are reaped later when profit is calculated. All developers should incorporate the necessity of this and software organizations to strengthen their products use this. There is no looking back!!!