Unit Testing


Unit testing is a way of testing individual units or components of a software. Unit testing basically involves breaking your program into individual pieces and subjecting each piece to a series of tests. a unit test is a method that instantiates a small portion of our application and verifies its behaviour independently from other parts. Tests are run periodically and also after a change is made to the source code to verify the functionality.

Typical unit test contains 3 phases: First, it initializes a small piece of an application it wants to test (also known as the system under test, or SUT), then it applies some stimulus to the system under test (usually by calling a method on it), and finally, it observes the resulting behaviour. If the observed behaviour is consistent with the expectations, the unit test passes, otherwise, it fails, indicating that there is a problem somewhere in the system under test.

Unit Testing Techniques

Unit testing techniques are catagorized into three parts,

  • White Box Testing: White box testing involves testing functional behaviour of the application.
  • Black Box Testing: Black box testing involves testing of user interface along with input and output.
  • Gray Box Testing: Gray box testing is used to execute test suites, test methods and performing risk assessment.

Unit Testing Tools

Most programming languages have their own unit testing framework that can be used to implement unit testing. Below are some of the most used unit testing frameworks for different languages.

  • Junit

JUnit is an open-source unit testing framework for a test-driven development environment. This framework is specifically designed for Java programming language

  • TestNG

TestNG is an open-source automation testing framework specifically designed for Java Programming language. TestNG resembles JUnit but offers some new functionality like concurrent testing to make it powerful and easier to use.

  • Embunit

Embunit is an open source unit testing framework which is designed for software application written in C or C++. Embunit is designed as a unit testing tool for both developers and testers for software application written in C or C++

  • HtmlUnit

HtmlUnit is an open-source unit testing framework that supports JavaScript and it provides GUI features like forms, links, tables, etc. It offers an open-source Java library which contains GUI-less browser for Java programs.

  • Unittest

The Python unit testing framework is a Python language version of JUnit,

  • PHPUnit

PHPUnit is a unit testing tool for PHP programmer. It takes small portions of code which is called units and test each of them separately.

Unittest implementation in Brainbox Framework

At Brainbox, we have developed a Test Automation Framework for end to end application testing of web and mobile android platforms. You can find the details about the framework here https://www.brainbox.consulting/blogs-news/software-testing-blog/brainbox-test-automation-framework/.

The framework is developed using Python as programming language. Hence we have adopted Unittest feature of Python to do unit testing. We have divided our framework libraries into smaller components and unit testing is implemented for each component and its methods to make sure every functionality of the framework works as expected.

We have made use of setUp() method that is available in Unittest framework, which the testing framework will automatically call for us when we run the test. The prerequisites needed to run a particular component is setup in this method and then the test runs for that component. We run the unit tests periodically and also after making changes to the code to verify everything is working properly.

Below are the things that are covered in unit testing of our framework,

  • The module interface is tested to ensure that information properly flows into and out of the program until under test.
  • Local data structure is examined to ensure that the data stored temporarily maintains its integrity during all steps in a programs execution.
  • All independent paths through the control structure are exercised to ensure that all statements in a module have been executed at least once.
  • Boundary conditions are tested to ensure that the module operates properly at boundaries established to limit or restrict processing.
  • All error-handling paths are tested.


  • Unit testing allows the programmer to refactor code at a later date, and make sure the module still works correctly (i.e. Regression testing). The procedure is to write test cases for all functions and methods so that whenever a change causes a fault, it can be quickly identified and fixed.
  • Doing unit tests is essentially doing quality assurance of the code. It shows problems and bugs before the product has an integration test. Creating a testing process before the coding is completed solves issues and challenges creators to write better code.
  • Unit testing helps to find problems and resolve them before further testing so they won’t impact other bits of code. This includes bugs in a programmer’s execution and issues with a specification for the unit itself.
  • Due to the modular nature of the unit testing, we can test parts of the project without waiting for others to be completed.
  • Using a unit test and good unit testing tools means you reduce the overall cost of a project. Early bug detection means fewer late changes and easier to spot issues than if it is done at a later stage.


  • With unit testing, you have to increase the amount of code that needs to be written. You usually have to write one or more unit tests depending on how complex things are. It is suggested to have at least three so you don’t just get a yes and a no that contradicts each other.
  • Unit testing cannot and will not catch all errors in a program. There is no way it can test every execution path or find integration errors and full system issues.
  • Unit testing by its very nature focuses on a unit of code. Hence it can’t catch integration errors or broad system level errors.
  • Unit tests are problematic when you need to test your user interface (UI). They are good for when you need to test business logic implementation but not great for UI.

Unit testing Best practices

1. Ensure Unit Tests are independent of each other:

While performing unit testing, make sure that all the unit tests are independent. If having any dependencies, then unit tests can get affected when there are any changes or enhancements. Also, it can result in complexities for the test cases to run and debug. Hence, always make sure that unit test cases are independent.

2. Always stick to one code unit at once:

When testing a unit of code, though it relates to multiple use cases, unit testers have to test each use case in different test cases. This will effectively simplify the teams to do code changes or refactoring.

3. Make use of AAA for readability:

AAA stands for Arrange, Act, and Assert. This pattern helps in separating what is being tested from the “arrange” and “assert” steps; thus reduces the inter-mix of the assertions with the help of the “Act”. Thus, the test cases are more readable.

4. Fix the bugs before moving for Integration Testing:

Unit testing is the first testing phase and it is practiced before moving to the phase of integration testing. Hence, before moving for the next testing level, make sure to fix all the identified bugs in the unit testing phase.

5. Ensure to have proper variable naming:

In unit testing, one of the important and best practices is to have proper naming of the variables. Therefore, avoid using magic strings, and also follow clear and consistent naming conventions.

6. Always separate test code and production code:

While performing unit testing, make sure that the test code is not deployed with the source code in your build script.


Unit testing  is very important to maintain the robustness of a software. A good implementation of unit testing will help find faults in the program at an early stage when some changes are made. It is important to run unit tests for different components of software to maintain its integrity and robustness.

Contact us at sales@brainbox.consulting for more information and we would be happy to assist you with testing related services.


Leave a comment

You must be logged in to post a comment.