What is Unit Testing?

Unit testing definition:

Unit testing is one of the software testing methodologies, where single units of code, modules, and components are tested individually and independently.


The purpose of unit testing to test the individual units before actually putting them to work.

The example of Unit Testing:

You have a program with four units, and there is some bug in the code. To determine where this bug is you will have to go through these four individual units and get them tested and you will also have to check whether the testing is done properly.

Here are some excellent points by Roy Osherove:

What is a Good Unit Test?

A good unit test is:

  • Able to be fully automated
  • Has full control over all the pieces running (Use mocks or stubs to achieve this isolation when needed)
  • Can be run in any order  if part of many other tests
  • Runs in memory (no DB or File access, for example)
  • Consistently returns the same result (You always run the same test, so no random numbers, for instance. Save those for integration or range tests)
  • Runs fast
  • Tests a single logical concept in the system
  • Readable
  • Maintainable
  • Trustworthy (when you see its result, you don’t need to debug the code just to be sure)

A best unit testing practice focuses on verification effort of the smallest unit of software design, i.e., the software component or module.

What is done in Unit Testing – Process

Unit testing is one of the many parts of software testing. Other tests like integration testing,  component testing, etc. are also part of software testing methodologies.

Here are the following tests which are carried out in software unit testing:

Local data structures are examined to ensure that data stored temporarily maintains its integrity during all steps in an algorithm’s 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 correctly, boundaries established to limit or restrict processing, and all error handling paths are tested.

Unit test procedures – Unit testing is normally considered as an adjunct to the coding step. The design of unit tests can be performed before coding begins or after source code has been generated. A review of design information guides establishing test cases that are likely to uncover errors in each of the categories discussed earlier. Each test case should be coupled with a set of expected results.

Here is an excellent and thorough explanation about unit testing in software engineering:

Read Quote of Syd Logan’s answer to Software Engineering: What is software unit testing and why is it important? On Quora

Benefits Of Unit Testing:

– They will speed up your development, since you don’t need to build the whole project.
– They will make you “fail fast”, which follows the agile methodology
– They will give to the confidence to refract your code.
– They will act as specification for the next guys who is going to maintain/develop your code.

If you go a one step further and follow TDD (Test Driven Development), tests can be used as a design tool to make your code focus of the features of the program and cut off the unnecessary boilerplate.

Know more & Get in the depth of testing: