White Box Testing
Testing is essential to software development to ensure the delivery of high-quality, error-free software products. Testers employ different software testing techniques to identify different issues, defects, and errors in software products. Among all white box testing is one. It is concerned with evaluating a software’s internal structure and implementation details.
This blog post will discuss the essential aspects of white box testing in software engineering.
Content
What is White-box testing?
White box testing is a software testing technique that tests a system’s internal design, source code structure, data structures used, and working details. Its primary objective is to improve the software’s design, input-output flow, usability, and security. It is also called transparent testing, structural testing, and glass box testing.
Implementing this testing technique requires testers to know the system’s code, architecture, and implementation details. Using this knowledge, they create test cases and execute them to verify the system’s correctness at the code level. Hence, it is also known as code-based testing.
Generally, developers perform white box testing. They have complete knowledge of the software’s source code and internal workings. However, in some cases, quality assurance (QA) professionals and testers who understand complex code can also do it.
This testing technique is called ‘White Box’ because developers or testers peek into a system’s internal workings from its outer shell.
It applies to the first three levels of software testing – unit testing and integration testing. Unit testing validates each software module independently. Subsequently, integration testing combines unit-tested modules logically and tests their interaction or communication.
Features
- Access to the Source Code: White box testing provides access to the software’s source code. This helps validate individual functions and modules.
- Code Coverage Analysis: Code coverage is a metric that determines the amount of code executed during testing. White box testing analyzes code coverage and uncovers untested source code areas.
- Detecting Logical Errors: It helps identify logical errors like infinite loops and incorrect conditional statements.
- Code Optimization: It detects performance issues, areas of code that need to be improved, and other issues. Developers or testers work to fix these issues and optimize the source code.
- Security Testing: Developers or testers can access the software’s source code and know its internal workings. Hence, they can identify security vulnerabilities.
What To Verify in White Box Testing?
White box testing in software testing evaluates the software’s source code to verify the following parameters:
- Internal security vulnerabilities.
- Each object, function, and statement of the source code individually.
- The functionality of conditional loops.
- Broken, incomplete, and poorly structured code paths.
- The input and output flow.
In short, this testing technique validates a software’s working flow. It involves providing a set of inputs and comparing the expected and actual outputs. If the actual output does not match the expected one, it results in an error or bug.
How to Perform White Box Testing?
Generally, developers or testers validate applications using this software testing technique in two steps:
- Analyze the Source Code – Understanding and analyzing an application’s source code is an elementary step. Testers or developers, whoever is performing testing, should have detailed knowledge of an application’s internal workings and the source code structure. In addition, they should consider implementing secure coding practices by keeping security as a primary factor.
This will help them create effective test cases to uncover potential security vulnerabilities and ensure the maximum percentage of test coverage.
- Create and Execute Test Cases – Testers or developers create and execute multiple small test cases to test the application’s single process. This ensures that the source code has a proper flow and structure. Well, this step requires extremely detailed knowledge of the source code. Hence developers usually carry it out.
White Box Testing Example
Now, we know that white box testing aims to verify the code structure, such as loop statements, conditional statements, decision branches, etc. We’ll understand it with a simple example. Consider the following code:
Test (a, b)
{
int n;
if (n % 2 == 0)
{
print("Even number")
}
else
{
print("Odd Number")
}
}
To validate this code, we have the following two test cases:
- n = 25
- n = 50
For the first test case, n = 25, the ‘if’ condition does not hold true. Hence, the program flow moves to the ‘else’ block and prints the statement inside it. For the second test case, n = 50, the ‘if’ condition holds true, and the statement inside it gets executed.
This way, white box testing has exercised each line of an application’s source code and uncovered potential code-level errors.
White Box Testing Techniques
The different types of white box testing techniques are as follows:
1. Statement Coverage
This technique requires traversing and testing each statement in the source code at least once. As a result, the entire source code gets exercised.
The statement coverage determines the percentage of the source code a specific set of test cases exercises. The formula for statement coverage is:
Statement Coverage = (Number of Statements Executed / Total Number of Statements) * 100
2. Decision Coverage/Branch Coverage
The best example of a branch (decision point) in programming is the ‘if’ statement. It has two branches – True and False. The branch coverage technique ensures that each branch in the source is executed at least once.
Branch coverage implies the percentage of branches or decision points executed during testing.
Branch Coverage = (Number of executed branches / Total number of branches) * 100%
3. Condition Coverage
Condition testing involves testing the individual conditions for both TRUE and FALSE outcomes. So, getting 100% condition coverage requires exercising each condition for both TRUE and FALSE results. For n conditions, we will have 2n test scripts.
The primary aim of condition coverage is to determine the output of each condition in the source code. However, it tests only those conditions with logical operands whose outcome is either true or false.
4. Multiple Condition Testing
It aims to test all the possible combinations of every condition in a branch. Let us understand this with an example.
Consider the following code:
if (A||B) print C |
The test cases for the above code will be:
- A=TRUE, B=TRUE
- A=TRUE, B=FALSE
- A=FALSE, B=TRUE
- A=FALSE, B=FALSE
Our example has 2 conditions – A and B, and 4 test cases. If there were 3 expressions, the number of test cases would be 8.
Hence for 100% coverage, we will have 2n test scripts. This is very exhaustive, and it is very difficult to achieve 100% coverage.
5. Path Testing
Path testing ensures that all possible paths in the source code are executed at least once. It involves creating a control flow graph using the source code or flowchart. Later, it determines the Cyclomatic complexity, which refers to independent paths. So, testers create minimal test cases for such independent paths.
Path Coverage = (Number paths exercised / Total Number of paths in the program) x 100 %
6. Loop Testing
Loops are common programming constructs and are used in most large programs. Testing loops is essential, as there are high chances of errors occurring at the start or end of loops. Hence, performing loop testing uncovers bugs or errors in any specific loop. The primary error encountered in loops is wrong indexes.
Types of White Box Testing
Here are different types of white box testing:
- Unit Testing: This is the first level of software testing. It tests an application’s every module, called a unit, individually for its correctness. It ensures that each component functions as expected.
- Integration Testing: This comes after integration testing. It combines unit-tested components logically and validates the interaction between them. It aims to uncover any errors in the interaction of components.
- White Box Penetration Testing: Testers have complete access to an application’s source code and network, IP, and server data, including passwords and maps. The primary goal of penetration testing is to uncover areas of the source code with security vulnerabilities.
- White Box Mutation Testing: As the name suggests, mutation testing depends on alterations. Testers perform minute modifications to the source code to check whether the execution of test cases on it uncovers any bugs. If test cases pass, it indicates an error in the source code. However, if test cases fail, the source code is error-free.
Advantages and Disadvantages of White Box Testing
Let us now shed light on the advantages and disadvantages of white box testing.
Advantages
- White box testing is comprehensive and detailed as it exercises every line of the source code.
- It identifies potential hidden errors, defects, and security vulnerabilities. Fixing them requires removing some lines of source code, which results in code optimization.
- It ensures that the source code complies with the coding standards and is performance-optimized.
- Even if the GUI is unavailable, testing starts early in the software development life cycle (SDLC).
- Test cases are easy to automate.
- The source code transparency helps determine the exact type of input data required for testing.
- Testers or developers can create test cases that can ensure maximum test coverage.
Disadvantages
- White box testing requires in-depth programming knowledge to understand and analyze a system’s source code and create test cases around it.
- It primarily focuses on testing the system’s internal workings and misses out on external issues.
- Large applications require a lot of time to undergo white box testing due to their lengthy source codes.
- A small change in the source code requires writing test cases again.
- There are strong chances of resulting in production errors.
White Box Testing Tools
Here is a list of some commonly used white box testing tools:
- Veracode: It provides a suite of tools that help identify and fix flaws in applications developed using different programming languages, such as .NET, C++, Java, etc. You can also test desktop and mobile applications for security.
- EclEmma: It is a free code coverage tool for Java applications. It was designed to run tests and analyze results within the Eclipse workbench.
- JSUnit.net: JSUnit is a component of JUnit, a unit testing framework for Java applications. JSUnit is an open-source unit testing tool for JavaScript testing. It is available under GNU General License 2.0.
- NUnit: It is a testing framework developed in C# to perform data-driven testing on .NET applications. It supports the parallel execution of tests without any manual intervention.
- CppUnit: It is also the component of JUnit as JSUnit. It is available for unit testing C++ applications.
Difference between White Box and Black Box testing
The following table highlights the primary differences between black box testing and white box testing:
White Box Testing | Black Box Testing |
Requires knowledge of an application’s internal structure and workings | Does not require any internal knowledge of an application. |
We can test many granular aspects of the application. | We test the end-to-end functionality of the application. |
Developers or QA professionals with sound knowledge of programming and application architecture perform this testing. | An independent QA team is in charge of performing black-box testing. |
Applicable for lower levels of testing – unit testing and integration testing. | Applicable to higher levels of testing – system testing and acceptance testing, where we must test the application as a whole. |
Conclusion
This was all about white box testing. It is a software testing technique that requires testers to have comprehensive knowledge of an application’s internal workings and code structure. Hence, it uncovers structural and security loopholes. The primary goal is to verify the application’s functionality and correctness at the code level.
Though this testing technique is time-consuming and requires much effort, it is the only way to ensure you test your source code’s every line. If performed correctly, white box testing significantly improves the software quality.