Table of contents
1.
Introduction
2.
Why Unit Testing
3.
A Real-world Example
4.
How to do Unit Testing
5.
Unit Testing Techniques
6.
Unit Testing LifeCycle
7.
Advantages and Disadvantages 
8.
Frequently Asked Questions
8.1.
Who can perform Unit Testing?
8.2.
What is the total number of phases in a Unit Test Case?
8.3.
What is the Junit testing framework?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

Unit Testing

Author ANJU JAISWAL
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Unit testing is Software Testing in which individual application units or components are tested. Individual units, i.e., computer program modules, usage methods, and operating procedures, are tested to see whether they are appropriate.

                                                                                                                       Source - link

The fundamental goal is to isolate each system component so that faults may be identified, analyzed, and fixed. A software system's unit is a single testable component tested during the application software's development phase and is usually done by the developers.

Why Unit Testing

Unit testing is the first layer of testing in a testing hierarchy, conducted before integration and the subsequent layers of testing.

Unit testing is crucial because software developers often try to save time by undertaking limited unit testing. This is a misconception since poor unit testing results in high-cost defect fixes during System and Integration Testing. Even after the program has been constructed, there is Beta testing. Unit testing is done early in the development process, saving time and money.

The following are the primary reasons for performing unit testing in software development:

  • Unit testing helps testers and developers to understand the base of code that makes them able to change defect-causing code quickly.
  • The documentation benefits from unit testing.
  • Unit testing identifies and corrects errors early in the development process, resulting in fewer defects in subsequent testing stages.
  • By relocating code and test cases, it aids with code reuse.

A Real-world Example

int Add(int a, int b) { return a+b; }
You can also try this code with Online C++ Compiler
Run Code

 

The function above takes two numbers and returns their sum.

The following is an example of unit test code:

void TestAdd1() { Assert.IsEqual(Add(5, 10), 15) }
You can also try this code with Online C++ Compiler
Run Code

 

The unit test above " asserts" that 5 + 10 equals 15. If the Add function returns anything different, Assert.IsEqual will fail the test case.

You'll almost certainly add a few more unit test cases, such as these:

void TestAdd2() { Assert.IsEqual(Add(500, 1000), 1500) }
void TestAdd3() { Assert.IsEqual(Add(0, 1000), 1000) }
void TestAdd4() { Assert.IsEqual(Add(-100, 100), 0) }
void TestAdd5() { Assert.IsEqual(Add(-100, -1100), -1200) }
You can also try this code with Online C++ Compiler
Run Code

 

You'll run your test cases after you've written them to ensure that everything is operating correctly.

Later, in addition to adding some of his code, another developer mistakenly changes the Add method to:

int Add(int a, int b) { return a*b; }
You can also try this code with Online C++ Compiler
Run Code

 

As you can see, the code now multiplies the two values rather than adding them. The unit tests you wrote for this function will fail when the developer executes them. The new developer can track down the failed test cases and correct the code.

How to do Unit Testing

During unit testing, developers write a section of code to test a specific function in a software application. Developers can also isolate this method to extensively test it, revealing and removing undesirable dependencies between the tested function and other units.

Unit Testing is of two types.

  • Manual
  • Automated

 

Unit testing is usually automated, although we can also do it manually. Automation is favoured, although Software Engineering does not favor one over the other. Unit testing can be done manually using a step-by-step instructional document. Some of the commonly used Unit Testing tools are Junit, Jtest, EMMA, NUnit, PHPUnit, etc.

Unit Testing Techniques

Unit testing uses all-white box testing techniques as it uses the code of software application:

  • Data flow testing
  • Control Flow Testing
  • Branch Coverage Testing
  • Statement Coverage Testing
  • Decision Coverage Testing

 

Also check out - Phases of Compiler

Unit Testing LifeCycle

                                                                                                                             Source - link

Advantages and Disadvantages 


Must Read Sanity Testing vs Smoke Testing

Frequently Asked Questions

Who can perform Unit Testing?

Unit testing is typically performed throughout the development phase so that developers can participate. Unit testing is typically conducted by automation engineers and QA professionals when developers are preoccupied with other development activities.

What is the total number of phases in a Unit Test Case?

A unit test case can be broken down into three stages. It will initialize the exact module of a software app that you want to test in the first stage. The test case will be successfully executed in the second stage. Finally, it will examine the finished product.

What is the Junit testing framework?

Several test cases must be run multiple times. The Junit framework can help you create test cases for repeated execution.

Conclusion

In this blog, we have given a detailed description of Unit Testing with a piece of real-world knowledge, how it works, and the importance of why we need unit testing. Also discussed various tools used in unit testing, its techniques, the pros and cons, and the diagram helps us represent the life cycle of unit testing.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem DesignMachine learning, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!!

Live masterclass