Unit Testing

Unit Testing in SDLC

In this article, I am going to discuss Unit Testing in SDLC. Please read our previous article where we discussed Non-Functional Testing. At the end of this article, you will understand the following important pointers which are related to Unit Testing in SDLC.

  1. What is Unit Testing?
  2. How Does Unit Testing Work?
  3. What are the Objectives of Unit Testing?
  4. What Kinds of Unit Testing are there?
  5. Manual vs. Automated Unit Testing
  6. Explain the Workflow of Unit Testing
  7. Who Conducts Unit Tests?
  8. Why is Unit Testing Necessary?
  9. Unit Testing Techniques
  10. Unit Testing Life Cycle in SDLC
  11. How to Achieve the Best Result via Unit Testing?
  12. What are the Advantages of Unit Testing?
  13. What are the Disadvantages of Unit Testing?
  14. Role of Unit Testing in QA Strategy
What is Unit Testing?

Unit testing is a type of software testing that concentrates on certain software system units or components. Each piece of software is examined through unit testing to ensure that it performs as intended and meets requirements. Unit testing is frequently done by developers early in the development process before the code is integrated and tested as a whole system.

To verify that the isolated code is correct, unit testing is performed. A unit component is a specific application function or section of code. Developers frequently carry out unit testing utilizing the white box testing approach. The test engineer will start independently or one-by-one testing each component of the module or module of the application once the application is finished and delivered to the test engineer. Unit testing or components testing is the two terms used to describe this process.

Testing individual program components is referred to as “Unit Testing” of programs. Unit testing on the program is done when creating an application. A technique or a particular function can be a single component. Unit testing is often done by the developer.

How Does Unit Testing Work?

Three steps make up a unit test: planning, developing test cases, and running the test itself. Developers or QA experts prepare and examine the unit test in the first stage. They then move on to writing test cases and scripts. The code is tested in the third stage. Unit tests must first be written that fail in order for test-driven development to function. They write code and refactor the application as soon as the test is successful. TDD often produces a codebase that is explicit and predictable.

To confirm that the code has no dependencies, each test case is run independently in an isolated environment. The software developer should utilize a testing framework to record any failed tests and code criteria to validate each test case. It would take a lot of time for developers to write tests for each line of code. The code that might influence how the program being created behaves should be the focus of the tests that developers write.

Unit testing only includes characteristics that are necessary for the tested unit to function. This enables developers to make modifications to the source code without worrying about how they might affect the operation of other components or the program as a whole right away. Teams can use integration testing to assess larger program components after every unit in the program operates as effectively and error-free as possible.

How Does Unit Testing Work?

What are the Objectives of Unit Testing?

Unit testing has the following goals:

  • To separate a piece of code.
  • To confirm the code’s accuracy.
  • To evaluate each process and function.
  • To save money and to fix bugs early in the development cycle.
  • To make it easier for the developers to comprehend the code base and to quickly implement modifications.
  • To facilitate code reuse.
What Kinds of Unit Testing are there?

Unit testing comes in two flavors: manual and automated.

Types of Unit Testing in SDLC

  1. Manual Testing: Without the use of automated tools or scripts, the tester performs test cases and runs test scenarios manually in the manual testing of software. A human tester is used to interact with the program, see how it behaves, and verify its functions using pre-defined test cases.
  2. Automated Testing: Automated testing is the process of running tests to check the functionality and accuracy of a software application or system using software tools and scripts. It entails creating test scripts or test cases that imitate user interactions and anticipated results, then automatically running these tests without human intervention.
Manual vs. Automated Unit Testing

Developers can perform unit tests manually or automatically. Those employing a manual method may have an instinctual document made detailing each step in the process; however, automated testing is the more common method for unit testing. Automated approaches commonly use a testing framework to develop test cases. These frameworks are also set to flag and report any failed test cases while also providing a summary of test cases.

Explain the Workflow of Unit Testing

Explain the Workflow of Unit Testing

There are typically four steps to this testing:

  • Making test cases or deciding what to test and how to test them is the first step.
  • The writing of the test code is the next phase.
  • The tests must be conducted as the third step. If they are successful, you can carry on because the code is operating as it should. If the tests are failed, the code has a flaw that must be resolved before moving forward.
  • The fourth step is to keep the tests running. You must update your tests to reflect changes to your code. That can entail altering or adding new test cases.
Who Conducts Unit Tests?

The first stage of software testing in the SDLC is known as unit testing, and it is typically started when an application is being developed. Software developers create and run these unit test scenarios. However, this white-box testing technique is carried out by QA engineers in a few uncommon cases where developers back off from this process.

Why is Unit Testing Necessary?

The initial stage of web application testing is frequently referred to as unit testing. Before publishing the software application, you must test any code you have written to make sure it is functioning as intended. Unit testing is therefore necessary for the following reasons:

  1. Early Bug Detection: Many errors are discovered early in the development cycle because unit testing is performed before integration testing.
  2. To Facilitate and Accelerate the Debugging Process: It would be simpler to find the bugs if you tested the individual units rather than the assembled modules. Facilitating and speeding up the debugging process.
  3. To Isolate a Piece of Code and Guarantee its Functionality: By enabling you to change the code without having an impact on other areas of your program, it improves code maintenance.
  4. To Check the Functionality of Your Application: Before incorporating a module into an application, it tests each individual unit. As a result, it guarantees that the code you create will function as needed.
  5. To Save Time and Money on Problem-Solving Expenses: Integration testing requires a lot of upkeep and is rather hard. Before advancing to integration, many errors are fixed if unit testing is in place. This reduces the overall cost and saves a lot of time.
Unit Testing Techniques:

There are 3 types of Unit Testing Techniques. They are

  1. Black Box Testing: This testing technique is used in covering the unit tests for input, user interface, and output parts.
  2. White Box Testing: This technique is used in testing the functional behavior of the system by giving the input and checking the functionality output including the internal design structure and code of the modules.
  3. Gray Box Testing: This technique is used in executing the relevant test cases, test methods, and test functions, and analyzing the code performance for the modules.
Unit Testing Life Cycle in SDLC

Unit testing is an essential component of the software development life cycle and is typically the initial phase. The six stages of the unit testing life cycle are as follows:

Unit Testing Life Cycle in SDLC

  1. Examine the Written Code: A unit test has a four-stage life cycle: planning, doing, reviewing, and maintaining. The unit testing life cycle dictates that you should first list the requirements for your code before attempting to develop a test case for each one. When you have finished implementing and testing the code, you review it.
  2. Make Appropriate Changes: When it’s time to restructure and update your code, quickly reviewing each function’s or method’s life cycle will help you understand what is happening there. Here’s an illustration: Passing in parameters, Code performing its duty, and Code that produces a result.
  3. Run the Test, Then Contrast the Outcomes with What was Anticipated: A test is developed during this stage of the unit testing life cycle by constructing a test object, choosing input values to run the test, running the test, and comparing the expected and actual outcomes.
  4. Fix the Code Problems that Have Been Found: Developers can feel at ease when adding or changing code thanks to unit testing because they know they will be alerted right away if they break anything. By doing this, you may address issues before they arise in production and affect end users.
  5. Rerun the Tests to Ensure they Passed: Unit testing is an excellent approach for developers to keep track of their changes, which can be particularly crucial when it comes to life cycle techniques that might not have a visual representation. Rerunning the tests to confirm they still function as intended after each update can be helpful. As you can see from this summary, unit testing can simplify your life by giving you the assurance that your modifications haven’t resulted in any functional regressions.
How to Achieve the Best Result via Unit Testing?

Unit testing can give the best results without getting confused and increasing complexity by following the steps listed below:

  • Test cases must be independent because if there is any change or enhancement in the requirement, the test cases will not be affected.
  • Naming conventions for unit test cases must be clear and consistent.
  • During unit testing, the identified bugs must be fixed before jumping on next phase of the SDLC.
  • Only one code should be tested at one time.
  • Adopt test cases with the writing of the code, if not doing so, the number of execution paths will be increased.
  • If there are changes in the code of any module, ensure the corresponding unit test is available or not for that module.
Tools for Unit Testing

Some of the tools are as follows:

  • JUnit
  • Jasmine
  • TestNG
  • PHPUnit
  • Mocha
Example of Unit Testing

Here is a straightforward Python programming example of unit testing in software testing. Consider the case where we have a function named add_numbers that receives two numbers as input and outputs their sum. In order to ensure that the function operates as intended, we want to create a unit test.
def add_numbers(a, b):
     return a + b
Let’s use Python’s unittest module to create a unit test for this function now:

import unittest
class TestAddNumbers(unittest.TestCase):
    def test_add_numbers(self):
        result = add_numbers(2, 3)
        self.assertEqual(result, 5)  # Verify that the result is equal to 5
if __name__ == '__main__':

The test case class TestAddNumbers, which derives from unittest, is defined in the code above.TestCase. We define the test_add_numbers method inside of this class, which calls the add_numbers function with the input values 2 and 3. The assertEqual method is then used to confirm that the result is equal to 5, as expected. Finally, we execute unittest.main() to perform the unit test. The TestAddNumbers class’s test methods will all be executed using this, and the test results will be shown.

If the add_numbers function is implemented properly, the unit test will run when this code is executed and passed. The test will fail in the absence of a defect in the code, revealing a problem with the add_numbers method.

This is a fundamental illustration of unit testing, in which we test a single function separately. Unit tests are helpful for confirming the accuracy of particular parts or units of code and aid in the early discovery of issues.

What are the Advantages of Unit Testing?

The advantages of Unit Testing are:

  1. Developers can discover what features a unit offers and how to utilize them by using unit testing to have a fundamental understanding of the unit API.
  2. Unit testing allows the programmer to enhance the code and guarantee that the module operates as intended.
  3. Specific project components can be tested using unit testing without having to wait for other project components to be done.
  4. Early Issue Detection: Unit testing enables developers to identify and address issues at an early stage of the development process, before they grow larger and become more challenging to address.
  5. Unit testing makes sure that each piece of code functions as intended and meets the requirements, which helps to enhance software quality overall.
What are the Disadvantages of Unit Testing?

The disadvantages of Unit Testing are:

  1. The process of writing the unit test cases takes time.
  2. Because there is a risk of faults in the modules during integration testing, unit testing won’t catch every issue in the module.
  3. Unit testing is ineffective for detecting flaws in the module’s UI (User Interface) section.
  4. Regular source code modifications increase the time required for maintenance.
  5. It cannot be used to test for non-functional factors like system performance and scalability.
Role of Unit Testing in QA Strategy

Unit testing has numerous advantages over other types of software testing techniques. By running unit tests, developers can get precise feedback and achieve high execution speed. Also, if you run unit tests that validate the functional behavior of an application, and the test fails, in most cases, you can ensure that the issue lies in the function

End-to-end testing and similar testing interact with the application just like a real user does. Therefore, it provides more realistic feedback. Furthermore, unit tests verify how different isolated modules or units function together. However, it can’t validate how these units integrate with other units. In this case, integration testing, end-to-end testing, and similar types of testing can verify how well these units integrate with different units.

The role of Unit testing in QA strategy is to provide fast and early feedback to developers. But you can’t rely entirely on unit tests as they lack some features in a few aspects. Therefore, an ideal approach is to back them with another type of testing in the area where unit tests fall short. Using a test pyramid can be a viable concept as it states that having a larger number of unit tests and fewer other types of tests is good.

In the next article, I am going to discuss Integration Testing in SDLC. Here, in this article, I try to explain Unit Testing in SDLC. I hope you enjoy this Unit Testing in SDLC article.

Leave a Reply

Your email address will not be published. Required fields are marked *