What is Unit testing and what benefits I will have if I apply it on my project

What is Unit Test:

In computer programming, unit testing is a software design and development method where the programmer verifies that individual units of source code are working properly. A unit is the smallest testable part of an application.

In OOP, the smallest unit is a method, which may belong to a base/super class, abstract class or derived/child class.

Unit testing is typically done by software developers to ensure that the code other developers have written meets software requirements and behaves as the developer intended.

Benefits of Applying Unit Test:

  • Using developer time for manual ad hoc testing is not a cost effective way of ensuring software quality. (Code coverage, breaking code by others, did I test all functionalities)
  • Human tester can and will make mistakes.. Even if Company have QA, discovering issues at QA side is more expensive than @ DEV side.
  • unit tests find problems early in the development cycle.
  • an automated unit test suite watches over your code in two dimensions: time and space
  • developers will be less afraid to change existing code
  • the development process becomes more flexible (Sending hotfixes more quickly)
  • Improves your project’s truck factor:

Truck factor is the minimum number of developers that if hit by a truck the project would grind to a halt. If you have one key developer who works on a subsystem that no-one else knows, then your truck factor is effectively “1”. Obviously, having truck factor of “1” is a risk on the project.

A comprehensive unit test suite improves truck factor because it makes it easier for a developer to take over a piece of code she is not intimately familiar with. A developer can start working on code written by others because the unit tests will guide the developer by pointing it out if she makes an error. Losing a key developer for any reason just before a release is less of a catastrophe if you have the safety net of unit tests.

  1. reduces the need for manual testing

Flavors of Unit testing:

  • Point unit tests exercise a small piece of functionality, e.g., just one method or methods of a class.
  • End-to-End tests exercise one feature across many, sometimes all, layers of an application; An ideal end-to-end test works like an end user story

Test Driven Programming model: Developer start developing test methods and basic functions that make test methods success, then enhance code at methods to make sure it will cover all expected/unexpected cases.

As a rule, before a developer checks in any code into the main source control repository, she should get the latest versions of the unit tests from the version control on her development machine and run all the tests locally. Only after all unit tests pass should the developer check in new code. This ensures that the source code in the version control system remains healthy at all times and can be used to rebuild the system at any time.

In real life, developers may accidentally check in code that does not work. The daily build should be your main line of defense against these bugs.

Real Word Facts:

Trusting unit tests blindly as in: "if unit tests pass, code is ready for production" is a recipe for shipping bugs. Even if we have a comprehensive unit test suite that is run frequently, bugs can still sneak in and some amount of manual testing by humans is needed.

What if Bug appear on Production:

We need to do "post mortem" analysis.. One of the following things turned out:

  1. We have 2 bugs: one inside actual Code and the other inside Unit test code.
  2. There is no unit test code that testing this functionality.
  3. Production environment is different from Test environment.

              Finally, If you have plenty of time at your project to apply Unit testing on it, the I would strongly recommend applying it.

Leave a Reply

Your email address will not be published.