logo logo

Unit Testing 101

Unit Testing 101

Software is made of hundreds of small components, each classified as a unit. Many times, due to time to market pressures, and in order to stay ahead of their competitors, testers ignore unit testing and focus on core functionality and UI/UX testing. This stops many bugs from coming to the surface, which in turn can cause serious damage to the product and the brand ❌

What Is Unit Testing And Why Is It A Must?

While the software is still under development, each completed unit is separated and tested to identify if it has any defects. If found, they are fixed then and there. Many times it involves only the QA, but sometimes the developer of the unit also takes part in the process.

This helps them understand the undesired behavior faster. They fix it quickly, and after retesting the bug no longer exists. Thus, we ensure that all building blocks or units are well tested before we connect them and perform integration or other functional tests. A unit can be a method, function, procedure, module, or a test class.

Developers are developers. They are very busy with coding aspects that they sometimes forget the fundamental principle of “testing early” and its benefits. If any bug is unearthed, they tend to fix it at the end, when the whole system is built.

A small bug found can become a huge flaw after all the integrations are done. If the testing, bug finding, and fixing can be shifted left, then the quality gets introduced at a very early phase in the system. Then the bugs can be found faster and the cost of fixing them is lower 💪

How To Do A “Better” Unit Testing

Unit testing itself has two approaches to perform it well:

  1. The manual approach
  2. The automation approach

In the manual approach, the QA engineers try to perform basic testing where they input the test data into the unit and then observe its behavior 👀 If the behavior is within the accepted range, then the test passes. Otherwise, it is failed. This testing also falls under the “black box testing” of a unit.

Or, they input the test data and then watch it transforming at each step of the code, thus looking at the code itself. This is called the “white box testing” approach. There is a third manual approach which is the “gray box testing” where the entire test suite and methods get executed. Out of three, the “white box” approach is the preferred way when performing unit testing manually.

The automation approach is more advanced and also preferred by the developers. Since the code is already written and the code itself is the System under Test (SUT), developers and automation testers find it simpler to create automation suites using the same code.

The developers write a piece of extra code within the system to test a function. Once it works fine, it can be commented or removed before deployments. The code can be moved out and put inside a unit test framework to test all its branches and dependencies. This helps in performing the following coverages of the unit and subsequent integration system:

  1. Branch coverage
  2. Decision coverage
  3. Statement coverage
  4. Condition coverage

Popular Unit Testing Frameworks

There are several unit testing frameworks on the market, free as well as licensed. Most are C and Java-based, but modern tools cater to all popular languages ✅ The following pictogram shows the popular tools:


Unit testing frameworks

Unit Test Best Practices

  1. Unit tests should be independent and not depend on other tests. Any requirement changes should not affect them. They should only be changed when the code changes.
  2. One unit test should cover one code piece.
  3. Just like any other tests, the naming conventions should be standardized across all unit cases.
  4. Fix unit test failures before moving to any integration. If not, it may lead to larger problems at a later stage.
  5. Keep testing as you finish one code. Any delay will make the unit tests more complex, as more user journeys need to be tested.

Advantages of Unit Testing

  1. Unit tests can provide a way for the developer to learn any existing code not written by him. Unit tests are the mirror of the code written.
  2. Refactoring a code is now easy. Since each unit test is bound to a code, when refactored, the unit case can be executed to test if the refactoring has broken down any piece of functionality.
  3. Unit tests give us independence in testing. The units can be tested independently without waiting for all other units to be completed. This saves time overall.

Disadvantages of Unit Testing

  1. Unit testing needs a mindset to test. Not all developers can understand its importance hence QA works in parallel with him. Two resources are required to test one Unit.
  2. Unit test is no magic. People think that if unit tests pass, then the application becomes bug-free. This is a myth and should be handled that way.
  3. Unit tests catch bugs at the code level and not at the integration or system level. Testing doesn’t stop at the unit testing level. People think this way and land in trouble later.


Unit testing is a must-have. We have seen various advantages, mainly early detection of issues that can potentially increase to larger issues as the system grows in size. However, due to quick time to market pressures, teams often ignore it, and just check end-to-end scenarios or other types of functional testing. We should avoid this practice and introduce unit tests in our daily work processes 💠

Vipin Jain

About the author

Vipin Jain

Vipin Jain has got 20 year experience in the IT industry. He has accumulated a deep knowledge in software projects, their methodologies and quality. He has dedicated the last 16 years of his professional career to Software Quality. Currently working with Metacube Software as Head QA and delivery manager, he is involved in establishing QCE at his company. An avid Speaker and writer, he loves speaking at conferences and delivered many presentations at national and international levels. He is member of Review Committees of various international organizations. He has presented papers in many of the best QA conferences across the world including TestingUnited, ExpoQA, QA&test and SQA, TestCon, TestingUY to name a few. Many of his papers got published in various magazines and journals across the world. He has a proven record of implementing and refining test processes for various clients across the globe.

Join TestProject Community

Get full access to the world's first cloud-based, open source friendly testing community. Enjoy TestProject's end-to-end test automation Platform, Forum, Blog and Docs - All for FREE.

Join Us Now  

Leave a Reply

popup image

A new world for test automation

Join 150,000 testing & dev teams taking their web & mobile testing to new heights, using #1 FREE test automation platform, designed to help deliver quality at speed.
Get Started